home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / font-lock.el.z / font-lock.el
Encoding:
Text File  |  1998-05-21  |  105.2 KB  |  2,610 lines

  1. ;;; font-lock.el --- decorating source files with fonts/colors based on syntax
  2.  
  3. ;; Copyright (C) 1992-1995, 1997 Free Software Foundation, Inc.
  4. ;; Copyright (C) 1995 Amdahl Corporation.
  5. ;; Copyright (C) 1996 Ben Wing.
  6.  
  7. ;; Author: Jamie Zawinski <jwz@netscape.com>, for the LISPM Preservation Society.
  8. ;; Minimally merged with FSF 19.34 by Barry Warsaw <bwarsaw@python.org>
  9. ;; Then (partially) synched with FSF 19.30, leading to:
  10. ;; Next Author: RMS
  11. ;; Next Author: Simon Marshall <simon@gnu.ai.mit.edu>
  12. ;; Latest XEmacs Author: Ben Wing
  13. ;; Maintainer: XEmacs Development Team (sigh :-( )
  14. ;; Keywords: languages, faces
  15.  
  16. ;; This file is part of XEmacs.
  17.  
  18. ;; XEmacs is free software; you can redistribute it and/or modify it
  19. ;; under the terms of the GNU General Public License as published by
  20. ;; the Free Software Foundation; either version 2, or (at your option)
  21. ;; any later version.
  22.  
  23. ;; XEmacs is distributed in the hope that it will be useful, but
  24. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  26. ;; General Public License for more details.
  27.  
  28. ;; You should have received a copy of the GNU General Public License
  29. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  30. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  31. ;; Boston, MA 02111-1307, USA.
  32.  
  33. ;;; Synched up with: FSF 19.30 except for the code to initialize the faces.
  34.  
  35. ;;; Commentary:
  36.  
  37. ;; Font-lock-mode is a minor mode that causes your comments to be
  38. ;; displayed in one face, strings in another, reserved words in another,
  39. ;; documentation strings in another, and so on.
  40. ;;
  41. ;; Comments will be displayed in `font-lock-comment-face'.
  42. ;; Strings will be displayed in `font-lock-string-face'.
  43. ;; Doc strings will be displayed in `font-lock-doc-string-face'.
  44. ;; Function and variable names (in their defining forms) will be
  45. ;;  displayed in `font-lock-function-name-face'.
  46. ;; Reserved words will be displayed in `font-lock-keyword-face'.
  47. ;;
  48. ;; Don't let the name fool you: you can highlight things using different
  49. ;; colors or background stipples instead of fonts, though that is not the
  50. ;; default.  See the variables `font-lock-use-colors' and
  51. ;; `font-lock-use-fonts' for broad control over this, or see the
  52. ;; documentation on faces and how to change their attributes for
  53. ;; fine-grained control.
  54. ;;
  55. ;; To make the text you type be fontified, use M-x font-lock-mode.  When
  56. ;; this minor mode is on, the fonts of the current line will be updated
  57. ;; with every insertion or deletion.
  58. ;;
  59. ;; By default, font-lock will automatically put newly loaded files
  60. ;; into font-lock-mode if it knows about the file's mode.  See the
  61. ;; variables `font-lock-auto-fontify', `font-lock-mode-enable-list',
  62. ;; and `font-lock-mode-disable-list' for control over this.
  63. ;;
  64. ;; The `font-lock-keywords' variable defines other patterns to highlight.
  65. ;; The default font-lock-mode-hook sets it to the value of the variables
  66. ;; lisp-font-lock-keywords, c-font-lock-keywords, etc, as appropriate.
  67. ;; The easiest way to change the highlighting patterns is to change the
  68. ;; values of c-font-lock-keywords and related variables.  See the doc
  69. ;; string of the variable `font-lock-keywords' for the appropriate syntax.
  70. ;;
  71. ;; The default value for `lisp-font-lock-keywords' is the value of the variable
  72. ;; `lisp-font-lock-keywords-1'.  You may like `lisp-font-lock-keywords-2' 
  73. ;; better; it highlights many more words, but is slower and makes your buffers
  74. ;; be very visually noisy.
  75. ;;
  76. ;; The same is true of `c-font-lock-keywords-1' and `c-font-lock-keywords-2';
  77. ;; the former is subdued, the latter is loud.
  78. ;;
  79. ;; You can make font-lock default to the gaudier variety of keyword
  80. ;; highlighting by setting the variable `font-lock-maximum-decoration'
  81. ;; before loading font-lock, or by calling the functions
  82. ;; `font-lock-use-default-maximal-decoration' or
  83. ;; `font-lock-use-default-minimal-decoration'.
  84. ;;
  85. ;; On a Sparc10, the initial fontification takes about 6 seconds for a typical
  86. ;; 140k file of C code, using the default configuration.  The actual speed
  87. ;; depends heavily on the type of code in the file, and how many non-syntactic
  88. ;; patterns match; for example, Xlib.h takes 23 seconds for 101k, because many
  89. ;; patterns match in it.  You can speed this up substantially by removing some
  90. ;; of the patterns that are highlighted by default.  Fontifying lisp code is
  91. ;; significantly faster, because lisp has a more regular syntax than C, so the
  92. ;; regular expressions don't have to be as complicated.
  93. ;;
  94. ;; It's called font-lock-mode here because on the Lispms it was called
  95. ;; "Electric Font Lock Mode."  It was called that because there was an older
  96. ;; mode called "Electric Caps Lock Mode" which had the function of causing all
  97. ;; of your source code to be in upper case except for strings and comments,
  98. ;; without you having to blip the caps lock key by hand all the time (thus the
  99. ;; "electric", as in `electric-c-brace'.)
  100.  
  101. ;; See also the related packages `fast-lock' and `lazy-lock'.  Both
  102. ;; attempt to speed up the initial fontification.  `fast-lock' saves
  103. ;; the fontification info when you exit Emacs and reloads it next time
  104. ;; you load the file, so that the file doesn't have to be fontified
  105. ;; again.  `lazy-lock' does "lazy" fontification -- i.e. it only
  106. ;; fontifies the text as it becomes visible rather than fontifying
  107. ;; the whole file when it's first loaded in.
  108.  
  109. ;; Further comments from the FSF:
  110.  
  111. ;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
  112. ;; are made thusly: (regexp-opt '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
  113. ;; efficiency.
  114.  
  115. ;; What is fontification for?  You might say, "It's to make my code look nice."
  116. ;; I think it should be for adding information in the form of cues.  These cues
  117. ;; should provide you with enough information to both (a) distinguish between
  118. ;; different items, and (b) identify the item meanings, without having to read
  119. ;; the items and think about it.  Therefore, fontification allows you to think
  120. ;; less about, say, the structure of code, and more about, say, why the code
  121. ;; doesn't work.  Or maybe it allows you to think less and drift off to sleep.
  122. ;;
  123. ;; So, here are my opinions/advice/guidelines:
  124. ;; 
  125. ;; - Use the same face for the same conceptual object, across all modes.
  126. ;;   i.e., (b) above, all modes that have items that can be thought of as, say,
  127. ;;   keywords, should be highlighted with the same face, etc.
  128. ;; - Keep the faces distinct from each other as far as possible.
  129. ;;   i.e., (a) above.
  130. ;; - Make the face attributes fit the concept as far as possible.
  131. ;;   i.e., function names might be a bold colour such as blue, comments might
  132. ;;   be a bright colour such as red, character strings might be brown, because,
  133. ;;   err, strings are brown (that was not the reason, please believe me).
  134. ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
  135. ;;   Only use OVERRIDE for special things that are easy to define, such as the
  136. ;;   way `...' quotes are treated in strings and comments in Emacs Lisp mode.
  137. ;;   Don't use it to, say, highlight keywords in commented out code or strings.
  138. ;; - Err, that's it.
  139.  
  140.  
  141. ;;; Code:
  142.  
  143. (require 'fontl-hooks)
  144.  
  145. ;;;;;;;;;;;;;;;;;;;;;;      user variables       ;;;;;;;;;;;;;;;;;;;;;;
  146.  
  147. (defvar font-lock-verbose t
  148.   "*If non-nil, means show status messages when fontifying.
  149. See also `font-lock-message-threshold'.")
  150.  
  151. (defvar font-lock-message-threshold 6000
  152.   "*Minimum size of region being fontified for status messages to appear.
  153.  
  154. The size is measured in characters.  This affects `font-lock-fontify-region'
  155. but not `font-lock-fontify-buffer'. (In other words, when you first visit
  156. a file and it gets fontified, you will see status messages no matter what
  157. size the file is.  However, if you do something else like paste a
  158. chunk of text or revert a buffer, you will see status messages only if the
  159. changed region is large enough.)
  160.  
  161. Note that setting `font-lock-verbose' to nil disables the status
  162. messages entirely.")
  163.  
  164. ;;;###autoload
  165. (defvar font-lock-auto-fontify t
  166.   "*Whether font-lock should automatically fontify files as they're loaded.
  167. This will only happen if font-lock has fontifying keywords for the major
  168. mode of the file.  You can get finer-grained control over auto-fontification
  169. by using this variable in combination with `font-lock-mode-enable-list' or
  170. `font-lock-mode-disable-list'.")
  171.  
  172. ;;;###autoload
  173. (defvar font-lock-mode-enable-list nil
  174.   "*List of modes to auto-fontify, if `font-lock-auto-fontify' is nil.")
  175.  
  176. ;;;###autoload
  177. (defvar font-lock-mode-disable-list nil
  178.   "*List of modes not to auto-fontify, if `font-lock-auto-fontify' is t.")
  179.  
  180. ;;;###autoload
  181. (defvar font-lock-use-colors '(color)
  182.   "*Specification for when Font Lock will set up color defaults.
  183. Normally this should be '(color), meaning that Font Lock will set up
  184. color defaults that are only used on color displays.  Set this to nil
  185. if you don't want Font Lock to set up color defaults at all.  This
  186. should be one of
  187.  
  188. -- a list of valid tags, meaning that the color defaults will be used
  189.    when all of the tags apply. (e.g. '(color x))
  190. -- a list whose first element is 'or and whose remaining elements are
  191.    lists of valid tags, meaning that the defaults will be used when
  192.    any of the tag lists apply.
  193. -- nil, meaning that the defaults should not be set up at all.
  194.  
  195. \(If you specify face values in your init file, they will override any
  196. that Font Lock specifies, regardless of whether you specify the face
  197. values before or after loading Font Lock.)
  198.  
  199. See also `font-lock-use-fonts'.  If you want more control over the faces
  200. used for fontification, see the documentation of `font-lock-mode' for
  201. how to do it.")
  202.  
  203. ;;;###autoload
  204. (defvar font-lock-use-fonts '(or (mono) (grayscale))
  205.   "*Specification for when Font Lock will set up non-color defaults.
  206.  
  207. Normally this should be '(or (mono) (grayscale)), meaning that Font
  208. Lock will set up non-color defaults that are only used on either mono
  209. or grayscale displays.  Set this to nil if you don't want Font Lock to
  210. set up non-color defaults at all.  This should be one of
  211.  
  212. -- a list of valid tags, meaning that the non-color defaults will be used
  213.    when all of the tags apply. (e.g. '(grayscale x))
  214. -- a list whose first element is 'or and whose remaining elements are
  215.    lists of valid tags, meaning that the defaults will be used when
  216.    any of the tag lists apply.
  217. -- nil, meaning that the defaults should not be set up at all.
  218.  
  219. \(If you specify face values in your init file, they will override any
  220. that Font Lock specifies, regardless of whether you specify the face
  221. values before or after loading Font Lock.)
  222.  
  223. See also `font-lock-use-colors'.  If you want more control over the faces
  224. used for fontification, see the documentation of `font-lock-mode' for
  225. how to do it.")
  226.  
  227. ;;;###autoload
  228. (defvar font-lock-maximum-decoration nil
  229.   "*If non-nil, the maximum decoration level for fontifying.
  230. If nil, use the minimum decoration (equivalent to level 0).
  231. If t, use the maximum decoration available.
  232. If a number, use that level of decoration (or if not available the maximum).
  233. If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
  234. where MAJOR-MODE is a symbol or t (meaning the default).  For example:
  235.  ((c++-mode . 2) (c-mode . t) (t . 1))
  236. means use level 2 decoration for buffers in `c++-mode', the maximum decoration
  237. available for buffers in `c-mode', and level 1 decoration otherwise.")
  238.  
  239. ;;;###autoload
  240. (define-obsolete-variable-alias 'font-lock-use-maximal-decoration
  241.   'font-lock-maximum-decoration)
  242.  
  243. ;;;###autoload
  244. (defvar font-lock-maximum-size (* 250 1024)
  245.   "*If non-nil, the maximum size for buffers for fontifying.
  246. Only buffers less than this can be fontified when Font Lock mode is turned on.
  247. If nil, means size is irrelevant.
  248. If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
  249. where MAJOR-MODE is a symbol or t (meaning the default).  For example:
  250.  ((c++-mode . 256000) (c-mode . 256000) (rmail-mode . 1048576))
  251. means that the maximum size is 250K for buffers in `c++-mode' or `c-mode', one
  252. megabyte for buffers in `rmail-mode', and size is irrelevant otherwise.")
  253.  
  254. ;; Fontification variables:
  255.  
  256. ;;;###autoload
  257. (defvar font-lock-keywords nil
  258.   "*A list of the keywords to highlight.
  259. Each element should be of the form:
  260.  
  261.  MATCHER
  262.  (MATCHER . MATCH)
  263.  (MATCHER . FACENAME)
  264.  (MATCHER . HIGHLIGHT)
  265.  (MATCHER HIGHLIGHT ...)
  266.  (eval . FORM)
  267.  
  268. where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
  269.  
  270. FORM is an expression, whose value should be a keyword element,
  271. evaluated when the keyword is (first) used in a buffer.  This feature
  272. can be used to provide a keyword that can only be generated when Font
  273. Lock mode is actually turned on.
  274.  
  275. For highlighting single items, typically only MATCH-HIGHLIGHT is required.
  276. However, if an item or (typically) items is to be highlighted following the
  277. instance of another item (the anchor) then MATCH-ANCHORED may be required.
  278.  
  279. MATCH-HIGHLIGHT should be of the form:
  280.  
  281.  (MATCH FACENAME OVERRIDE LAXMATCH)
  282.  
  283. Where MATCHER can be either the regexp to search for, a variable
  284. containing the regexp to search for, or the function to call to make
  285. the search (called with one argument, the limit of the search).  MATCH
  286. is the subexpression of MATCHER to be highlighted.  FACENAME is either
  287. a symbol naming a face, or an expression whose value is the face name
  288. to use.  If you want FACENAME to be a symbol that evaluates to a face,
  289. use a form like \"(progn sym)\".
  290.  
  291. OVERRIDE and LAXMATCH are flags.  If OVERRIDE is t, existing fontification may
  292. be overwritten.  If `keep', only parts not already fontified are highlighted.
  293. If `prepend' or `append', existing fontification is merged with the new, in
  294. which the new or existing fontification, respectively, takes precedence.
  295. If LAXMATCH is non-nil, no error is signalled if there is no MATCH in MATCHER.
  296.  
  297. For example, an element of the form highlights (if not already highlighted):
  298.  
  299.  \"\\\\\\=<foo\\\\\\=>\"        Discrete occurrences of \"foo\" in the value of the
  300.             variable `font-lock-keyword-face'.
  301.  (\"fu\\\\(bar\\\\)\" . 1)    Substring \"bar\" within all occurrences of \"fubar\" in
  302.             the value of `font-lock-keyword-face'.
  303.  (\"fubar\" . fubar-face)    Occurrences of \"fubar\" in the value of `fubar-face'.
  304.  (\"foo\\\\|bar\" 0 foo-bar-face t)
  305.             Occurrences of either \"foo\" or \"bar\" in the value
  306.             of `foo-bar-face', even if already highlighted.
  307.  
  308. MATCH-ANCHORED should be of the form:
  309.  
  310.  (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
  311.  
  312. Where MATCHER is as for MATCH-HIGHLIGHT with one exception.  The limit of the
  313. search is currently guaranteed to be (no greater than) the end of the line.
  314. PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
  315. the last, instance MATCH-ANCHORED's MATCHER is used.  Therefore they can be
  316. used to initialise before, and cleanup after, MATCHER is used.  Typically,
  317. PRE-MATCH-FORM is used to move to some position relative to the original
  318. MATCHER, before starting with MATCH-ANCHORED's MATCHER.  POST-MATCH-FORM might
  319. be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
  320.  
  321. For example, an element of the form highlights (if not already highlighted):
  322.  
  323.  (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
  324.  
  325.  Discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
  326.  discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
  327.  (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil.  Therefore \"item\" is
  328.  initially searched for starting from the end of the match of \"anchor\", and
  329.  searching for subsequent instance of \"anchor\" resumes from where searching
  330.  for \"item\" concluded.)
  331.  
  332. Note that the MATCH-ANCHORED feature is experimental; in the future, we may
  333. replace it with other ways of providing this functionality.
  334.  
  335. These regular expressions should not match text which spans lines.  While
  336. \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
  337. when you edit the buffer does not, since it considers text one line at a time.
  338.  
  339. Be very careful composing regexps for this list;
  340. the wrong pattern can dramatically slow things down!")
  341. ;;;###autoload
  342. (make-variable-buffer-local 'font-lock-keywords)
  343.  
  344. (defvar font-lock-defaults nil
  345.   "The defaults font Font Lock mode for the current buffer.
  346. Normally, do not set this directly.  If you are writing a major mode,
  347. put a property of `font-lock-defaults' on the major-mode symbol with
  348. the desired value.
  349.  
  350. It should be a list
  351.  
  352. \(KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN)
  353.  
  354. KEYWORDS may be a symbol (a variable or function whose value is the keywords
  355. to use for fontification) or a list of symbols.  If KEYWORDS-ONLY is non-nil,
  356. syntactic fontification (strings and comments) is not performed.  If CASE-FOLD
  357. is non-nil, the case of the keywords is ignored when fontifying.  If
  358. SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form (CHAR
  359. . STRING) used to set the local Font Lock syntax table, for keyword and
  360. syntactic fontification (see `modify-syntax-entry').
  361.  
  362. If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
  363. backwards outside any enclosing syntactic block, for syntactic fontification.
  364. Typical values are `beginning-of-line' (i.e., the start of the line is known to
  365. be outside a syntactic block), or `beginning-of-defun' for programming modes or
  366. `backward-paragraph' for textual modes (i.e., the mode-dependent function is
  367. known to move outside a syntactic block).  If nil, the beginning of the buffer
  368. is used as a position outside of a syntactic block, in the worst case.
  369.  
  370. These item elements are used by Font Lock mode to set the variables
  371. `font-lock-keywords', `font-lock-keywords-only',
  372. `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
  373. `font-lock-beginning-of-syntax-function', respectively.
  374.  
  375. Alternatively, if the value is a symbol, it should name a major mode,
  376. and the defaults for that mode will apply.")
  377. (make-variable-buffer-local 'font-lock-defaults)
  378.  
  379. ;; FSF uses `font-lock-defaults-alist' and expects the major mode to
  380. ;; set a value for `font-lock-defaults', but I don't like either of
  381. ;; these -- requiring the mode to set `font-lock-defaults' makes it
  382. ;; impossible to have defaults for a minor mode, and using an alist is
  383. ;; generally a bad idea for information that really should be
  384. ;; decentralized. (Who knows what strange modes might want
  385. ;; font-locking?)
  386.  
  387. (defvar font-lock-keywords-only nil
  388.   "Non-nil means Font Lock should not do syntactic fontification.
  389. This is normally set via `font-lock-defaults'.
  390.  
  391. This should be nil for all ``language'' modes, but other modes, like
  392. dired, do not have anything useful in the syntax tables (no comment
  393. or string delimiters, etc) and so there is no need to use them and
  394. this variable should have a value of t.
  395.  
  396. You should not set this variable directly; its value is computed
  397. from `font-lock-defaults', or (if that does not specify anything)
  398. by examining the syntax table to see whether it appears to contain
  399. anything useful.")
  400. (make-variable-buffer-local 'font-lock-keywords-only)
  401.  
  402. (defvar font-lock-keywords-case-fold-search nil
  403.   "Whether the strings in `font-lock-keywords' should be case-folded.
  404. This variable is automatically buffer-local, as the correct value depends
  405. on the language in use.")
  406. (make-variable-buffer-local 'font-lock-keywords-case-fold-search)
  407.  
  408. (defvar font-lock-after-fontify-buffer-hook nil
  409.   "Function or functions to run after completion of font-lock-fontify-buffer.")
  410.  
  411. (defvar font-lock-syntax-table nil
  412.   "Non-nil means use this syntax table for fontifying.
  413. If this is nil, the major mode's syntax table is used.
  414. This is normally set via `font-lock-defaults'.")
  415. (make-variable-buffer-local 'font-lock-syntax-table)
  416.  
  417. ;; These are used in the FSF version in syntactic font-locking.
  418. ;; We do this all in C.
  419. ;;; These record the parse state at a particular position, always the
  420. ;;; start of a line.  Used to make
  421. ;;; `font-lock-fontify-syntactically-region' faster.
  422. ;(defvar font-lock-cache-position nil)
  423. ;(defvar font-lock-cache-state nil)
  424. ;(make-variable-buffer-local 'font-lock-cache-position)
  425. ;(make-variable-buffer-local 'font-lock-cache-state)
  426.  
  427. ;; If this is nil, we only use the beginning of the buffer if we can't use
  428. ;; `font-lock-cache-position' and `font-lock-cache-state'.
  429. (defvar font-lock-beginning-of-syntax-function nil
  430.   "Non-nil means use this function to move back outside of a syntactic block.
  431. If this is nil, the beginning of the buffer is used (in the worst case).
  432. This is normally set via `font-lock-defaults'.")
  433. (make-variable-buffer-local 'font-lock-beginning-of-syntax-function)
  434.  
  435. ;;;###autoload
  436. (defvar font-lock-mode nil) ; for modeline
  437. (defvar font-lock-fontified nil) ; whether we have hacked this buffer
  438. (put 'font-lock-fontified 'permanent-local t)
  439.  
  440. ;;;###autoload
  441. (defvar font-lock-mode-hook nil
  442.   "Function or functions to run on entry to font-lock-mode.")
  443.  
  444. ; whether font-lock-set-defaults has already been run.
  445. (defvar font-lock-defaults-computed nil)
  446. (make-variable-buffer-local 'font-lock-defaults-computed)
  447.  
  448. ;; #### barf gag retch.  Horrid FSF lossage that we need to
  449. ;; keep around for compatibility with font-lock-keywords that
  450. ;; forget to properly quote their faces.
  451. (defvar font-lock-comment-face 'font-lock-comment-face
  452.   "Don't even think of using this.")
  453. (defvar font-lock-doc-string-face 'font-lock-doc-string-face
  454.   "Don't even think of using this.")
  455. (defvar font-lock-string-face 'font-lock-string-face
  456.   "Don't even think of using this.")
  457. (defvar font-lock-keyword-face 'font-lock-keyword-face
  458.   "Don't even think of using this.")
  459. (defvar font-lock-function-name-face 'font-lock-function-name-face
  460.   "Don't even think of using this.")
  461. (defvar font-lock-variable-name-face 'font-lock-variable-name-face
  462.   "Don't even think of using this.")
  463. (defvar font-lock-type-face 'font-lock-type-face
  464.   "Don't even think of using this.")
  465. (defvar font-lock-reference-face 'font-lock-reference-face
  466.   "Don't even think of using this.")
  467. (defvar font-lock-preprocessor-face 'font-lock-preprocessor-face
  468.   "Don't even think of using this.")
  469.  
  470.  
  471. ;;;;;;;;;;;;;;;;;;;;;;        actual code        ;;;;;;;;;;;;;;;;;;;;;;
  472.  
  473. ;;; To fontify the whole buffer by language syntax, we go through it a
  474. ;;; character at a time, creating extents on the boundary of each syntactic
  475. ;;; unit (that is, one extent for each block comment, one for each line
  476. ;;; comment, one for each string, etc.)  This is done with the C function
  477. ;;; syntactically-sectionize.  It's in C for speed (the speed of lisp function
  478. ;;; calls was a real bottleneck for this task since it involves examining each
  479. ;;; character in turn.)
  480. ;;;
  481. ;;; Then we make a second pass, to fontify the buffer based on other patterns
  482. ;;; specified by regexp.  When we find a match for a region of text, we need
  483. ;;; to change the fonts on those characters.  This is done with the
  484. ;;; put-text-property function, which knows how to efficiently share extents.
  485. ;;; Conceptually, we are attaching some particular face to each of the
  486. ;;; characters in a range, but the implementation of this involves creating
  487. ;;; extents, or resizing existing ones.
  488. ;;;
  489. ;;; Each time a modification happens to a line, we re-fontify the entire line.
  490. ;;; We do this by first removing the extents (text properties) on the line,
  491. ;;; and then doing the syntactic and keyword passes again on that line.  (More
  492. ;;; generally, each modified region is extended to include the preceding and
  493. ;;; following BOL or EOL.)
  494. ;;;
  495. ;;; This means that, as the user types, we repeatedly go back to the beginning
  496. ;;; of the line, doing more work the longer the line gets.  This doesn't cost
  497. ;;; much in practice, and if we don't, then we incorrectly fontify things when,
  498. ;;; for example, inserting spaces into `intfoo () {}'.
  499. ;;;
  500.  
  501.  
  502. ;; The user level functions
  503.  
  504. ;;;###autoload
  505. (defun font-lock-mode (&optional arg)
  506.   "Toggle Font Lock Mode.
  507. With arg, turn font-lock mode on if and only if arg is positive.
  508.  
  509. When Font Lock mode is enabled, text is fontified as you type it:
  510.  
  511.  - Comments are displayed in `font-lock-comment-face';
  512.  - Strings are displayed in `font-lock-string-face';
  513.  - Documentation strings (in Lisp-like languages) are displayed in
  514.    `font-lock-doc-string-face';
  515.  - Language keywords (\"reserved words\") are displayed in
  516.    `font-lock-keyword-face';
  517.  - Function names in their defining form are displayed in
  518.    `font-lock-function-name-face';
  519.  - Variable names in their defining form are displayed in
  520.    `font-lock-variable-name-face';
  521.  - Type names are displayed in `font-lock-type-face';
  522.  - References appearing in help files and the like are displayed
  523.    in `font-lock-reference-face';
  524.  - Preprocessor declarations are displayed in
  525.   `font-lock-preprocessor-face';
  526.  
  527.    and
  528.  
  529.  - Certain other expressions are displayed in other faces according
  530.    to the value of the variable `font-lock-keywords'.
  531.  
  532. Where modes support different levels of fontification, you can use the variable
  533. `font-lock-maximum-decoration' to specify which level you generally prefer.
  534. When you turn Font Lock mode on/off the buffer is fontified/defontified, though
  535. fontification occurs only if the buffer is less than `font-lock-maximum-size'.
  536. To fontify a buffer without turning on Font Lock mode, and regardless of buffer
  537. size, you can use \\[font-lock-fontify-buffer].
  538.  
  539. See the variable `font-lock-keywords' for customization."
  540.   (interactive "P")
  541.   (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode)))
  542.     (maximum-size (if (not (consp font-lock-maximum-size))
  543.               font-lock-maximum-size
  544.             (cdr (or (assq major-mode font-lock-maximum-size)
  545.                  (assq t font-lock-maximum-size))))))
  546.     ;; Font-lock mode will refuse to turn itself on if in batch mode, or if
  547.     ;; the current buffer is "invisible".  The latter is because packages
  548.     ;; sometimes put their temporary buffers into some particular major mode
  549.     ;; to get syntax tables and variables and whatnot, but we don't want the
  550.     ;; fact that the user has font-lock-mode on a mode hook to slow these
  551.     ;; things down.
  552.     (if (or noninteractive (eq (aref (buffer-name) 0) ?\ ))
  553.     (setq on-p nil))
  554.     (if (equal (buffer-name) " *Compiler Input*") ; hack for bytecomp...
  555.     (setq on-p nil))
  556.     (cond (on-p
  557.        (make-local-hook 'after-change-functions)
  558.        (add-hook 'after-change-functions
  559.              'font-lock-after-change-function nil t)
  560.        (add-hook 'pre-idle-hook 'font-lock-pre-idle-hook))
  561.       (t
  562.        (remove-hook 'after-change-functions
  563.             'font-lock-after-change-function t)
  564.        (setq font-lock-defaults-computed nil
  565.          font-lock-keywords nil)
  566.        ;; We have no business doing this here, since 
  567.        ;; pre-idle-hook is global.    Other buffers may
  568.        ;; still be in font-lock mode.  -dkindred@cs.cmu.edu
  569.        ;; (remove-hook 'pre-idle-hook 'font-lock-pre-idle-hook)
  570.        ))
  571.     (set (make-local-variable 'font-lock-mode) on-p)
  572.     (cond (on-p
  573.        (font-lock-set-defaults-1)
  574.        (make-local-hook 'before-revert-hook)
  575.        (make-local-hook 'after-revert-hook)
  576.        ;; If buffer is reverted, must clean up the state.
  577.        (add-hook 'before-revert-hook 'font-lock-revert-setup nil t)
  578.        (add-hook 'after-revert-hook 'font-lock-revert-cleanup nil t)
  579.        (run-hooks 'font-lock-mode-hook)
  580.        (cond (font-lock-fontified
  581.           nil)
  582.          ((or (null maximum-size) (<= (buffer-size) maximum-size))
  583.           (font-lock-fontify-buffer))
  584.          (font-lock-verbose
  585.           (display-message
  586.            'command
  587.            (format "Fontifying %s... buffer too big." (buffer-name))))))
  588.       (font-lock-fontified
  589.        (setq font-lock-fontified nil)
  590.        (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
  591.        (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
  592.        (font-lock-unfontify-region (point-min) (point-max))
  593.        (font-lock-thing-lock-cleanup))
  594.       (t
  595.        (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
  596.        (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
  597.        (font-lock-thing-lock-cleanup)))
  598.     (redraw-modeline)))
  599.  
  600. ;; For init-file hooks
  601. ;;;###autoload
  602. (defun turn-on-font-lock ()
  603.   "Unconditionally turn on Font Lock mode."
  604.   (font-lock-mode 1))
  605.  
  606. ;;;###autoload
  607. (defun turn-off-font-lock ()
  608.   "Unconditionally turn off Font Lock mode."
  609.   (font-lock-mode 0))
  610.  
  611. ;;;###autoload
  612. (defun font-lock-fontify-buffer ()
  613.   "Fontify the current buffer the way `font-lock-mode' would.
  614. See `font-lock-mode' for details.
  615.  
  616. This can take a while for large buffers."
  617.   (interactive)
  618.   (let ((was-on font-lock-mode)
  619.     (font-lock-verbose (or font-lock-verbose (interactive-p)))
  620.     (font-lock-message-threshold 0)
  621.     (aborted nil))
  622.     ;; Turn it on to run hooks and get the right font-lock-keywords.
  623.     (or was-on (font-lock-mode 1))
  624.     (font-lock-unfontify-region (point-min) (point-max) t)
  625. ;;    (buffer-syntactic-context-flush-cache)
  626.     
  627.     ;; If a ^G is typed during fontification, abort the fontification, but
  628.     ;; return normally (do not signal.)  This is to make it easy to abort
  629.     ;; fontification if it's taking a long time, without also causing the
  630.     ;; buffer not to pop up.  If a real abort is desired, the user can ^G
  631.     ;; again.
  632.     ;;
  633.     ;; Possibly this should happen down in font-lock-fontify-region instead
  634.     ;; of here, but since that happens from the after-change-hook (meaning
  635.     ;; much more frequently) I'm afraid of the bad consequences of stealing
  636.     ;; the interrupt character at inopportune times.
  637.     ;;
  638.     (condition-case nil
  639.     (save-excursion
  640.       (font-lock-fontify-region (point-min) (point-max)))
  641.       (quit
  642.        (setq aborted t)))
  643.  
  644.     (or was-on        ; turn it off if it was off.
  645.     (let ((font-lock-fontified nil)) ; kludge to prevent defontification
  646.       (font-lock-mode 0)))
  647.     (set (make-local-variable 'font-lock-fontified) t)
  648.     (if (and aborted font-lock-verbose)
  649.     (display-message 'command
  650.              (format "Fontifying %s... aborted." (buffer-name))))
  651.     )
  652.   (run-hooks 'font-lock-after-fontify-buffer-hook))
  653.  
  654. ;; Fontification functions.
  655.  
  656. ;; We first define some defsubsts to encapsulate the way we add
  657. ;; faces to a region of text.  I am planning on modifying the
  658. ;; text-property mechanism so that multiple independent classes
  659. ;; of text properties can exist.  That way, for example, ediff's
  660. ;; face text properties don't interfere with font lock's face
  661. ;; text properties.  Due to the XEmacs implementation of text
  662. ;; properties in terms of extents, doing this is fairly trivial:
  663. ;; instead of using the `text-prop' property, you just use a
  664. ;; specified property.
  665.  
  666. (defsubst font-lock-set-face (start end face)
  667.   ;; Set the face on the characters in the range.
  668.   (put-nonduplicable-text-property start end 'face face)
  669.   (put-nonduplicable-text-property start end 'font-lock t))
  670.  
  671. (defsubst font-lock-remove-face (start end)
  672.   ;; Remove any syntax highlighting on the characters in the range.
  673.   (put-nonduplicable-text-property start end 'face nil)
  674.   (put-nonduplicable-text-property start end 'font-lock nil))
  675.  
  676. (defsubst font-lock-any-faces-p (start end)
  677.   ;; Return non-nil if we've put any syntax highlighting on
  678.   ;; the characters in the range.
  679.   ;;
  680.   ;; used to look for 'text-prop property, but this has problems if
  681.   ;; you put any other text properties in the vicinity.  Simon
  682.   ;; Marshall suggested looking for the 'face property (this is what
  683.   ;; FSF Emacs does) but that's equally bogus.  Only reliable way is
  684.   ;; for font-lock to specially mark its extents.
  685.   ;;
  686.   ;; FSF's (equivalent) definition of this defsubst would be
  687.   ;; (text-property-not-all start end 'font-lock nil)
  688.   ;;
  689.   ;; Perhaps our `map-extents' is faster than our definition
  690.   ;; of `text-property-not-all'.  #### If so, `text-property-not-all'
  691.   ;; should be fixed ...
  692.   ;;
  693.   (map-extents 'extent-property (current-buffer) start (1- end) 'font-lock))
  694.  
  695.  
  696. ;; Fontification functions.
  697.  
  698. ;; We use this wrapper.  However, `font-lock-fontify-region' used to be the
  699. ;; name used for `font-lock-fontify-syntactically-region', so a change isn't
  700. ;; back-compatible.  But you shouldn't be calling these directly, should you?
  701. (defun font-lock-fontify-region (beg end &optional loudly)
  702.   (let ((modified (buffer-modified-p))
  703.     (buffer-undo-list t) (inhibit-read-only t)
  704.     (old-syntax-table (syntax-table))
  705.     buffer-file-name buffer-file-truename)
  706.     (unwind-protect
  707.     (progn
  708.       ;; Use the fontification syntax table, if any.
  709.       (if font-lock-syntax-table (set-syntax-table font-lock-syntax-table))
  710.       ;; Now do the fontification.
  711.       (if font-lock-keywords-only
  712.           (font-lock-unfontify-region beg end)
  713.         (font-lock-fontify-syntactically-region beg end loudly))
  714.       (font-lock-fontify-keywords-region beg end loudly))
  715.       ;; Clean up.
  716.       (set-syntax-table old-syntax-table)
  717.       (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
  718.  
  719. ;; The following must be rethought, since keywords can override fontification.
  720. ;      ;; Now scan for keywords, but not if we are inside a comment now.
  721. ;      (or (and (not font-lock-keywords-only)
  722. ;           (let ((state (parse-partial-sexp beg end nil nil 
  723. ;                        font-lock-cache-state)))
  724. ;         (or (nth 4 state) (nth 7 state))))
  725. ;      (font-lock-fontify-keywords-region beg end))
  726.  
  727. (defun font-lock-unfontify-region (beg end &optional maybe-loudly)
  728.   (if (and maybe-loudly font-lock-verbose
  729.        (>= (- end beg) font-lock-message-threshold))
  730.       (display-message
  731.        'progress
  732.        (format "Fontifying %s..." (buffer-name))))
  733.   (let ((modified (buffer-modified-p))
  734.     (buffer-undo-list t) (inhibit-read-only t)
  735.     buffer-file-name buffer-file-truename)
  736.     (font-lock-remove-face beg end)
  737.     (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil))))
  738.  
  739. ;; Following is the original FSF version (similar to our original
  740. ;; version, before all the crap I added below).
  741. ;;
  742. ;; Probably that crap should either be fixed up so it works better,
  743. ;; or tossed away.
  744. ;;
  745. ;; I think that lazy-lock v2 tries to do something similar.
  746. ;; Those efforts should be merged.
  747.  
  748. ;; Called when any modification is made to buffer text.
  749. ;(defun font-lock-after-change-function (beg end old-len)
  750. ;  (save-excursion
  751. ;    (save-match-data
  752. ;      ;; Rescan between start of line from `beg' and start of line after `end'.
  753. ;      (font-lock-fontify-region
  754. ;    (progn (goto-char beg) (beginning-of-line) (point))
  755. ;    (progn (goto-char end) (forward-line 1) (point))))))
  756.  
  757. (defvar font-lock-old-extent nil)
  758. (defvar font-lock-old-len 0)
  759.  
  760. (defun font-lock-fontify-glumped-region ()
  761.   ;; even if something goes wrong in the fontification, mark the glumped
  762.   ;; region as fontified; otherwise, the same error might get signaled
  763.   ;; after every command.
  764.   (unwind-protect
  765.       ;; buffer may be deleted.
  766.       (if (buffer-live-p (extent-object font-lock-old-extent))
  767.       (save-excursion
  768.         (set-buffer (extent-object font-lock-old-extent))
  769.         (font-lock-after-change-function-1
  770.          (extent-start-position font-lock-old-extent)
  771.          (extent-end-position font-lock-old-extent)
  772.          font-lock-old-len)))
  773.     (detach-extent font-lock-old-extent)
  774.     (setq font-lock-old-extent nil)))
  775.  
  776. (defun font-lock-pre-idle-hook ()
  777.   (condition-case nil
  778.       (if font-lock-old-extent
  779.       (font-lock-fontify-glumped-region))
  780.     (error (warn "Error caught in `font-lock-pre-idle-hook'"))))
  781.  
  782. (defvar font-lock-always-fontify-immediately nil
  783.   "Set this to non-nil to disable font-lock deferral.")
  784.  
  785. ;;; called when any modification is made to buffer text.  This function
  786. ;;; attempts to glump adjacent changes together so that excessive
  787. ;;; fontification is avoided.  This function could easily be adapted
  788. ;;; to other after-change-functions.
  789.  
  790. (defun font-lock-after-change-function (beg end old-len)
  791.   (let ((obeg (and font-lock-old-extent
  792.            (extent-start-position font-lock-old-extent)))
  793.     (oend (and font-lock-old-extent
  794.            (extent-end-position font-lock-old-extent)))
  795.     (bc-end (+ beg old-len)))
  796.  
  797.     ;; If this change can't be merged into the glumped one,
  798.     ;; we need to fontify the glumped one right now.
  799.     (if (and font-lock-old-extent
  800.          (or (not (eq (current-buffer)
  801.               (extent-object font-lock-old-extent)))
  802.          (< bc-end obeg)
  803.          (> beg oend)))
  804.     (font-lock-fontify-glumped-region))
  805.   
  806.     (if font-lock-old-extent
  807.     ;; Update glumped region.
  808.     (progn
  809.       ;; Any characters in the before-change region that are
  810.       ;; outside the glumped region go into the glumped
  811.       ;; before-change region.
  812.       (if (> bc-end oend)
  813.           (setq font-lock-old-len (+ font-lock-old-len (- bc-end oend))))
  814.       (if (> obeg beg)
  815.           (setq font-lock-old-len (+ font-lock-old-len (- obeg beg))))
  816.       ;; New glumped region is the union of the glumped region
  817.       ;; and the new region.
  818.       (set-extent-endpoints font-lock-old-extent
  819.                 (min obeg beg)
  820.                 (max oend end)))
  821.  
  822.       ;; No glumped region, so create one.
  823.       (setq font-lock-old-extent (make-extent beg end))
  824.       (set-extent-property font-lock-old-extent 'detachable nil)
  825.       (set-extent-property font-lock-old-extent 'end-open nil)
  826.       (setq font-lock-old-len old-len))
  827.  
  828.     (if font-lock-always-fontify-immediately
  829.     (font-lock-fontify-glumped-region))))
  830.  
  831. (defun font-lock-after-change-function-1 (beg end old-len)
  832.   (if (null font-lock-mode)
  833.       nil
  834.     (save-excursion
  835.       (save-restriction
  836.     ;; if we don't widen, then fill-paragraph (and any command that
  837.     ;; operates on a narrowed region) confuses things, because the C
  838.     ;; code will fail to realize that we're inside a comment.
  839.     (widen)
  840.     (save-match-data
  841.       (let ((zmacs-region-stays zmacs-region-stays)) ; protect from change!
  842.         (goto-char beg)
  843.         ;; Maybe flush the internal cache used by syntactically-sectionize.
  844.         ;; (It'd be nice if this was more automatic.)  Any deletions mean
  845.         ;; the cache is invalid, and insertions at beginning or end of line
  846.         ;; mean that the bol cache might be invalid.
  847. ;;        (if (or (> old-len 0) (bobp) (= (preceding-char) ?\n))
  848. ;;        (buffer-syntactic-context-flush-cache))
  849.  
  850.         ;; Always recompute the whole line.
  851.         (goto-char end)
  852.         (forward-line 1)
  853.         (setq end (point))
  854.         (goto-char beg)
  855.         (beginning-of-line)
  856.         (setq beg (point))
  857.         ;; Rescan between start of line from `beg' and start of line after
  858.         ;; `end'.
  859.         (font-lock-fontify-region beg end)))))))
  860.  
  861.  
  862. ;; Syntactic fontification functions.
  863.  
  864. ;; Note: Here is the FSF version.  Our version is much faster because
  865. ;; of the C support we provide.  This may be useful for reference,
  866. ;; however, and perhaps there is something useful here that should
  867. ;; be merged into our version.
  868. ;;
  869. ;(defun font-lock-fontify-syntactically-region (start end &optional loudly)
  870. ;  "Put proper face on each string and comment between START and END.
  871. ;START should be at the beginning of a line."
  872. ;  (let ((synstart (if comment-start-skip
  873. ;               (concat "\\s\"\\|" comment-start-skip)
  874. ;             "\\s\""))
  875. ;     (comstart (if comment-start-skip
  876. ;               (concat "\\s<\\|" comment-start-skip)
  877. ;             "\\s<"))
  878. ;     state prev prevstate)
  879. ;    (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
  880. ;    (save-restriction
  881. ;      (widen)
  882. ;      (goto-char start)
  883. ;      ;;
  884. ;      ;; Find the state at the `beginning-of-line' before `start'.
  885. ;      (if (eq start font-lock-cache-position)
  886. ;       ;; Use the cache for the state of `start'.
  887. ;       (setq state font-lock-cache-state)
  888. ;     ;; Find the state of `start'.
  889. ;     (if (null font-lock-beginning-of-syntax-function)
  890. ;         ;; Use the state at the previous cache position, if any, or
  891. ;         ;; otherwise calculate from `point-min'.
  892. ;         (if (or (null font-lock-cache-position)
  893. ;             (< start font-lock-cache-position))
  894. ;         (setq state (parse-partial-sexp (point-min) start))
  895. ;           (setq state (parse-partial-sexp font-lock-cache-position start
  896. ;                           nil nil font-lock-cache-state)))
  897. ;       ;; Call the function to move outside any syntactic block.
  898. ;       (funcall font-lock-beginning-of-syntax-function)
  899. ;       (setq state (parse-partial-sexp (point) start)))
  900. ;     ;; Cache the state and position of `start'.
  901. ;     (setq font-lock-cache-state state
  902. ;           font-lock-cache-position start))
  903. ;      ;;
  904. ;      ;; If the region starts inside a string, show the extent of it.
  905. ;      (if (nth 3 state)
  906. ;       (let ((beg (point)))
  907. ;         (while (and (re-search-forward "\\s\"" end 'move)
  908. ;             (nth 3 (parse-partial-sexp beg (point)
  909. ;                            nil nil state))))
  910. ;         (put-text-property beg (point) 'face font-lock-string-face)
  911. ;         (setq state (parse-partial-sexp beg (point) nil nil state))))
  912. ;      ;;
  913. ;      ;; Likewise for a comment.
  914. ;      (if (or (nth 4 state) (nth 7 state))
  915. ;       (let ((beg (point)))
  916. ;         (save-restriction
  917. ;           (narrow-to-region (point-min) end)
  918. ;           (condition-case nil
  919. ;           (progn
  920. ;             (re-search-backward comstart (point-min) 'move)
  921. ;             (forward-comment 1)
  922. ;             ;; forward-comment skips all whitespace,
  923. ;             ;; so go back to the real end of the comment.
  924. ;             (skip-chars-backward " \t"))
  925. ;         (error (goto-char end))))
  926. ;         (put-text-property beg (point) 'face font-lock-comment-face)
  927. ;         (setq state (parse-partial-sexp beg (point) nil nil state))))
  928. ;      ;;
  929. ;      ;; Find each interesting place between here and `end'.
  930. ;      (while (and (< (point) end)
  931. ;           (setq prev (point) prevstate state)
  932. ;           (re-search-forward synstart end t)
  933. ;           (progn
  934. ;             ;; Clear out the fonts of what we skip over.
  935. ;             (remove-text-properties prev (point) '(face nil))
  936. ;             ;; Verify the state at that place
  937. ;             ;; so we don't get fooled by \" or \;.
  938. ;             (setq state (parse-partial-sexp prev (point)
  939. ;                             nil nil state))))
  940. ;     (let ((here (point)))
  941. ;       (if (or (nth 4 state) (nth 7 state))
  942. ;           ;;
  943. ;           ;; We found a real comment start.
  944. ;           (let ((beg (match-beginning 0)))
  945. ;         (goto-char beg)
  946. ;         (save-restriction
  947. ;           (narrow-to-region (point-min) end)
  948. ;           (condition-case nil
  949. ;               (progn
  950. ;             (forward-comment 1)
  951. ;             ;; forward-comment skips all whitespace,
  952. ;             ;; so go back to the real end of the comment.
  953. ;             (skip-chars-backward " \t"))
  954. ;             (error (goto-char end))))
  955. ;         (put-text-property beg (point) 'face
  956. ;                    font-lock-comment-face)
  957. ;         (setq state (parse-partial-sexp here (point) nil nil state)))
  958. ;         (if (nth 3 state)
  959. ;         ;;
  960. ;         ;; We found a real string start.
  961. ;         (let ((beg (match-beginning 0)))
  962. ;           (while (and (re-search-forward "\\s\"" end 'move)
  963. ;                   (nth 3 (parse-partial-sexp here (point)
  964. ;                              nil nil state))))
  965. ;           (put-text-property beg (point) 'face font-lock-string-face)
  966. ;           (setq state (parse-partial-sexp here (point)
  967. ;                           nil nil state))))))
  968. ;     ;;
  969. ;     ;; Make sure `prev' is non-nil after the loop
  970. ;     ;; only if it was set on the very last iteration.
  971. ;     (setq prev nil)))
  972. ;    ;;
  973. ;    ;; Clean up.
  974. ;    (and prev (remove-text-properties prev end '(face nil)))))
  975.  
  976. (defun font-lock-fontify-syntactically-region (start end &optional loudly)
  977.   "Put proper face on each string and comment between START and END.
  978. START should be at the beginning of a line."
  979.   (if font-lock-keywords-only
  980.       nil
  981.     (if (and font-lock-verbose
  982.          (>= (- end start) font-lock-message-threshold))
  983.     (display-message
  984.      'progress
  985.      (format "Fontifying %s... (syntactically...)" (buffer-name))))
  986.     (font-lock-unfontify-region start end loudly)
  987.     (goto-char start)
  988.     (if (> end (point-max)) (setq end (point-max)))
  989.     (syntactically-sectionize
  990.       #'(lambda (s e context depth)
  991.       (let (face)
  992.         (cond ((eq context 'string)
  993.            ;;#### Should only do this is Lisp-like modes!
  994.            (setq face
  995.              (if (= depth 1)
  996.                  ;; really we should only use this if
  997.                  ;;  in position 3 depth 1, but that's
  998.                  ;;  too expensive to compute.
  999.                  'font-lock-doc-string-face
  1000.                'font-lock-string-face)))
  1001.           ((or (eq context 'comment)
  1002.                (eq context 'block-comment))
  1003.            (setq face 'font-lock-comment-face)
  1004. ;         ;; Don't fontify whitespace at the beginning of lines;
  1005. ;         ;;  otherwise comment blocks may not line up with code.
  1006. ;         ;; (This is sometimes a good idea, sometimes not; in any
  1007. ;         ;; event it should be in C for speed --jwz)
  1008. ;         (save-excursion
  1009. ;             (goto-char s)
  1010. ;             (while (prog1 (search-forward "\n" (1- e) 'move)
  1011. ;                  (setq face 'font-lock-comment-face)
  1012. ;                  (setq e (point)))
  1013. ;               (skip-chars-forward " \t\n")
  1014. ;               (setq s (point)))
  1015.            ))
  1016.         (font-lock-set-face s e face)))
  1017.       start end)
  1018.     ))
  1019.  
  1020. ;;; Additional text property functions.
  1021.  
  1022. ;; The following three text property functions are not generally available (and
  1023. ;; it's not certain that they should be) so they are inlined for speed.
  1024. ;; The case for `fillin-text-property' is simple; it may or not be generally
  1025. ;; useful.  (Since it is used here, it is useful in at least one place.;-)
  1026. ;; However, the case for `append-text-property' and `prepend-text-property' is
  1027. ;; more complicated.  Should they remove duplicate property values or not?  If
  1028. ;; so, should the first or last duplicate item remain?  Or the one that was
  1029. ;; added?  In our implementation, the first duplicate remains.
  1030.  
  1031. ;; XEmacs: modified all these functions to use
  1032. ;; `put-nonduplicable-text-property' instead of `put-text-property', and
  1033. ;; the first one to take both SETPROP and MARKPROP, in accordance with the
  1034. ;; changed definitions of `font-lock-any-faces-p' and `font-lock-set-face'.
  1035.  
  1036. (defsubst font-lock-fillin-text-property (start end setprop markprop value &optional object)
  1037.   "Fill in one property of the text from START to END.
  1038. Arguments PROP and VALUE specify the property and value to put where none are
  1039. already in place.  Therefore existing property values are not overwritten.
  1040. Optional argument OBJECT is the string or buffer containing the text."
  1041.   (let ((start (text-property-any start end markprop nil object)) next)
  1042.     (while start
  1043.       (setq next (next-single-property-change start markprop object end))
  1044.       (put-nonduplicable-text-property start next setprop value object)
  1045.       (put-nonduplicable-text-property start next markprop value object)
  1046.       (setq start (text-property-any next end markprop nil object)))))
  1047.  
  1048. ;; This function (from simon's unique.el) is rewritten and inlined for speed.
  1049. ;(defun unique (list function)
  1050. ;  "Uniquify LIST, deleting elements using FUNCTION.
  1051. ;Return the list with subsequent duplicate items removed by side effects.
  1052. ;FUNCTION is called with an element of LIST and a list of elements from LIST,
  1053. ;and should return the list of elements with occurrences of the element removed,
  1054. ;i.e., a function such as `delete' or `delq'.
  1055. ;This function will work even if LIST is unsorted.  See also `uniq'."
  1056. ;  (let ((list list))
  1057. ;    (while list
  1058. ;      (setq list (setcdr list (funcall function (car list) (cdr list))))))
  1059. ;  list)
  1060.  
  1061. (defsubst font-lock-unique (list)
  1062.   "Uniquify LIST, deleting elements using `delq'.
  1063. Return the list with subsequent duplicate items removed by side effects."
  1064.   (let ((list list))
  1065.     (while list
  1066.       (setq list (setcdr list (delq (car list) (cdr list))))))
  1067.   list)
  1068.  
  1069. ;; A generalisation of `facemenu-add-face' for any property, but without the
  1070. ;; removal of inactive faces via `facemenu-discard-redundant-faces' and special
  1071. ;; treatment of `default'.  Uses `unique' to remove duplicate property values.
  1072. (defsubst font-lock-prepend-text-property (start end prop value &optional object)
  1073.   "Prepend to one property of the text from START to END.
  1074. Arguments PROP and VALUE specify the property and value to prepend to the value
  1075. already in place.  The resulting property values are always lists, and unique.
  1076. Optional argument OBJECT is the string or buffer containing the text."
  1077.   (let ((val (if (listp value) value (list value))) next prev)
  1078.     (while (/= start end)
  1079.       (setq next (next-single-property-change start prop object end)
  1080.         prev (get-text-property start prop object))
  1081.       (put-text-property
  1082.        start next prop
  1083.        (font-lock-unique (append val (if (listp prev) prev (list prev))))
  1084.        object)
  1085.       (setq start next))))
  1086.  
  1087. (defsubst font-lock-append-text-property (start end prop value &optional object)
  1088.   "Append to one property of the text from START to END.
  1089. Arguments PROP and VALUE specify the property and value to append to the value
  1090. already in place.  The resulting property values are always lists, and unique.
  1091. Optional argument OBJECT is the string or buffer containing the text."
  1092.   (let ((val (if (listp value) value (list value))) next prev)
  1093.     (while (/= start end)
  1094.       (setq next (next-single-property-change start prop object end)
  1095.         prev (get-text-property start prop object))
  1096.       (put-text-property
  1097.        start next prop
  1098.        (font-lock-unique (append (if (listp prev) prev (list prev)) val))
  1099.        object)
  1100.       (setq start next))))
  1101.  
  1102. ;;; Regexp fontification functions.
  1103.  
  1104. (defsubst font-lock-apply-highlight (highlight)
  1105.   "Apply HIGHLIGHT following a match.
  1106. HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
  1107.   (let* ((match (nth 0 highlight))
  1108.      (start (match-beginning match)) (end (match-end match))
  1109.      (override (nth 2 highlight)))
  1110.     (let ((newface (nth 1 highlight)))
  1111.       (or (symbolp newface)
  1112.       (setq newface (eval newface)))
  1113.       (cond ((not start)
  1114.          ;; No match but we might not signal an error.
  1115.          (or (nth 3 highlight)
  1116.          (error "No match %d in highlight %S" match highlight)))
  1117.         ((= start end) nil)
  1118.         ((not override)
  1119.          ;; Cannot override existing fontification.
  1120.          (or (font-lock-any-faces-p start end)
  1121.          (font-lock-set-face start end newface)))
  1122.         ((eq override t)
  1123.          ;; Override existing fontification.
  1124.          (font-lock-set-face start end newface))
  1125.         ((eq override 'keep)
  1126.          ;; Keep existing fontification.
  1127.          (font-lock-fillin-text-property start end 'face 'font-lock
  1128.                          newface))
  1129.         ((eq override 'prepend)
  1130.          ;; Prepend to existing fontification.
  1131.          (font-lock-prepend-text-property start end 'face newface))
  1132.         ((eq override 'append)
  1133.          ;; Append to existing fontification.
  1134.          (font-lock-append-text-property start end 'face newface))))))
  1135.  
  1136. (defsubst font-lock-fontify-anchored-keywords (keywords limit)
  1137.   "Fontify according to KEYWORDS until LIMIT.
  1138. KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords'."
  1139.   (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights)
  1140.     ;; Until we come up with a cleaner solution, we make LIMIT the end of line.
  1141.     (save-excursion (end-of-line) (setq limit (min limit (point))))
  1142.     ;; Evaluate PRE-MATCH-FORM.
  1143.     (eval (nth 1 keywords))
  1144.     (save-match-data
  1145.       ;; Find an occurrence of `matcher' before `limit'.
  1146.       (while (if (stringp matcher)
  1147.          (re-search-forward matcher limit t)
  1148.            (funcall matcher limit))
  1149.     ;; Apply each highlight to this instance of `matcher'.
  1150.     (setq highlights lowdarks)
  1151.     (while highlights
  1152.       (font-lock-apply-highlight (car highlights))
  1153.       (setq highlights (cdr highlights)))))
  1154.     ;; Evaluate POST-MATCH-FORM.
  1155.     (eval (nth 2 keywords))))
  1156.  
  1157. (defun font-lock-fontify-keywords-region (start end &optional loudvar)
  1158.   "Fontify according to `font-lock-keywords' between START and END.
  1159. START should be at the beginning of a line."
  1160.   (let ((loudly (and font-lock-verbose
  1161.              (>= (- end start) font-lock-message-threshold))))
  1162.     (let ((case-fold-search font-lock-keywords-case-fold-search)
  1163.       (keywords (cdr (if (eq (car-safe font-lock-keywords) t)
  1164.                  font-lock-keywords
  1165.                (font-lock-compile-keywords))))
  1166.       (bufname (buffer-name)) (count 0)
  1167.       keyword matcher highlights)
  1168.       ;;
  1169.       ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
  1170.       (while keywords
  1171.     (if loudly (display-message
  1172.             'progress
  1173.             (format "Fontifying %s... (regexps..%s)" bufname
  1174.                 (make-string (setq count (1+ count)) ?.))))
  1175.     ;;
  1176.     ;; Find an occurrence of `matcher' from `start' to `end'.
  1177.     (setq keyword (car keywords) matcher (car keyword))
  1178.     (goto-char start)
  1179.     (while (and (< (point) end)
  1180.             (if (stringp matcher)
  1181.             (re-search-forward matcher end t)
  1182.               (funcall matcher end)))
  1183.       ;; Apply each highlight to this instance of `matcher', which may be
  1184.       ;; specific highlights or more keywords anchored to `matcher'.
  1185.       (setq highlights (cdr keyword))
  1186.       (while highlights
  1187.         (if (numberp (car (car highlights)))
  1188.         (let ((end (match-end (car (car highlights)))))
  1189.           (font-lock-apply-highlight (car highlights))
  1190.           ;; restart search just after the end of the
  1191.           ;; keyword so keywords can share bracketing
  1192.           ;; expressions.
  1193.           (and end (goto-char end)))
  1194.           (font-lock-fontify-anchored-keywords (car highlights) end))
  1195.         (setq highlights (cdr highlights))))
  1196.     (setq keywords (cdr keywords))))
  1197.     (if loudly (display-message
  1198.         'progress
  1199.         (format "Fontifying %s... done." (buffer-name))))))
  1200.  
  1201.  
  1202. ;; Various functions.
  1203.  
  1204. ;; Turn off other related packages if they're on.  I prefer a hook. --sm.
  1205. ;; These explicit calls are easier to understand
  1206. ;; because people know what they will do.
  1207. ;; A hook is a mystery because it might do anything whatever. --rms.
  1208. (defun font-lock-thing-lock-cleanup ()
  1209.   (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
  1210.      (fast-lock-mode -1))
  1211.     ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
  1212.      (lazy-lock-mode -1))
  1213.     ((and (boundp 'lazy-shot-mode) lazy-shot-mode)
  1214.      (lazy-shot-mode -1))))
  1215.  
  1216. ;; Do something special for these packages after fontifying.  I prefer a hook.
  1217. (defun font-lock-after-fontify-buffer ()
  1218.   (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
  1219.      (fast-lock-after-fontify-buffer))
  1220.     ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
  1221.      (lazy-lock-after-fontify-buffer))))
  1222.  
  1223. ;; If the buffer is about to be reverted, it won't be fontified afterward.
  1224. (defun font-lock-revert-setup ()
  1225.   (setq font-lock-fontified nil))
  1226.  
  1227. ;; If the buffer has just been reverted, normally that turns off
  1228. ;; Font Lock mode.  So turn the mode back on if necessary.
  1229. (defalias 'font-lock-revert-cleanup 'turn-on-font-lock)
  1230.  
  1231.  
  1232. (defun font-lock-compile-keywords (&optional keywords)
  1233.   ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD
  1234.   ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string.
  1235.   (let ((keywords (or keywords font-lock-keywords)))
  1236.     (setq font-lock-keywords 
  1237.      (if (eq (car-safe keywords) t)
  1238.      keywords
  1239.        (cons t (mapcar 'font-lock-compile-keyword keywords))))))
  1240.  
  1241. (defun font-lock-compile-keyword (keyword)
  1242.   (cond ((nlistp keyword)        ; Just MATCHER
  1243.      (list keyword '(0 font-lock-keyword-face)))
  1244.     ((eq (car keyword) 'eval)    ; Specified (eval . FORM)
  1245.      (font-lock-compile-keyword (eval (cdr keyword))))
  1246.     ((numberp (cdr keyword))    ; Specified (MATCHER . MATCH)
  1247.      (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
  1248.     ((symbolp (cdr keyword))    ; Specified (MATCHER . FACENAME)
  1249.      (list (car keyword) (list 0 (cdr keyword))))
  1250.     ((nlistp (nth 1 keyword))    ; Specified (MATCHER . HIGHLIGHT)
  1251.      (list (car keyword) (cdr keyword)))
  1252.     (t                ; Hopefully (MATCHER HIGHLIGHT ...)
  1253.      keyword)))
  1254.  
  1255. (defun font-lock-choose-keywords (keywords level)
  1256.   ;; Return LEVELth element of KEYWORDS.  A LEVEL of nil is equal to a
  1257.   ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
  1258.   (let ((level (if (not (consp level))
  1259.            level
  1260.          (cdr (or (assq major-mode level) (assq t level))))))
  1261.     (cond ((symbolp keywords)
  1262.        keywords)
  1263.       ((numberp level)
  1264.        (or (nth level keywords) (car (reverse keywords))))
  1265.       ((eq level t)
  1266.        (car (reverse keywords)))
  1267.       (t
  1268.        (car keywords)))))
  1269.  
  1270.  
  1271. ;;; Determining which set of font-lock keywords to use.
  1272.  
  1273. (defun font-lock-find-font-lock-defaults (modesym)
  1274.   ;; Get the defaults based on the major mode.
  1275.   (let (raw-defaults)
  1276.     ;; I want a do-while loop!
  1277.     (while (progn
  1278.          (setq raw-defaults (get modesym 'font-lock-defaults))
  1279.          (and raw-defaults (symbolp raw-defaults)
  1280.           (setq modesym raw-defaults)))
  1281.       )
  1282.     raw-defaults))
  1283.  
  1284. (defun font-lock-examine-syntax-table ()
  1285.   ; Computes the value of font-lock-keywords-only for this buffer.
  1286.   (if (eq (syntax-table) (standard-syntax-table))
  1287.       ;; Assume that modes which haven't bothered to install their own
  1288.       ;; syntax table don't do anything syntactically interesting.
  1289.       ;; Really, the standard-syntax-table shouldn't have comments and
  1290.       ;; strings in it, but changing that now might break things.
  1291.       nil
  1292.     ;; else map over the syntax table looking for strings or comments.
  1293.     (let (got-one)
  1294.       ;; XEmacs 20.0 ...
  1295.       (if (fboundp 'map-syntax-table)
  1296.       (setq got-one
  1297.         (map-syntax-table
  1298.          #'(lambda (key value)
  1299.              (memq (char-syntax-from-code value)
  1300.                '(?\" ?\< ?\> ?\$)))
  1301.          (syntax-table)))
  1302.     ;; older Emacsen.
  1303.     (let ((i (1- (length (syntax-table)))))
  1304.       (while (>= i 0)
  1305.         (if (memq (char-syntax i) '(?\" ?\< ?\> ?\$))
  1306.         (setq got-one t i 0))
  1307.         (setq i (1- i)))))
  1308.       (set (make-local-variable 'font-lock-keywords-only) (not got-one)))))
  1309.  
  1310. ;; font-lock-set-defaults is in fontl-hooks.el.
  1311.  
  1312. ;;;###autoload
  1313. (defun font-lock-set-defaults-1 (&optional explicit-defaults)
  1314.   ;; does everything that font-lock-set-defaults does except
  1315.   ;; enable font-lock-mode.  This is called by `font-lock-mode'.
  1316.   ;; Note that the return value is used!
  1317.  
  1318.   (if (and font-lock-defaults-computed (not explicit-defaults))
  1319.       ;; nothing to do.
  1320.       nil
  1321.  
  1322.     (or font-lock-keywords
  1323.     (let* ((defaults (or (and (not (eq t explicit-defaults))
  1324.                   explicit-defaults)
  1325.                  ;; in case modes decide to set
  1326.                  ;; `font-lock-defaults' themselves,
  1327.                  ;; as in FSF Emacs.
  1328.                  font-lock-defaults
  1329.                  (font-lock-find-font-lock-defaults major-mode)))
  1330.            (keywords (font-lock-choose-keywords
  1331.               (nth 0 defaults) font-lock-maximum-decoration)))
  1332.       
  1333.       ;; Keywords?
  1334.       (setq font-lock-keywords (if (fboundp keywords)
  1335.                        (funcall keywords)
  1336.                      (eval keywords)))
  1337.       (or font-lock-keywords
  1338.           ;; older way:
  1339.           ;; try to look for a variable `foo-mode-font-lock-keywords',
  1340.           ;; or similar.
  1341.           (let ((major (symbol-name major-mode))
  1342.             (try #'(lambda (n)
  1343.                  (if (stringp n) (setq n (intern-soft n)))
  1344.                  (if (and n
  1345.                       (boundp n))
  1346.                  n
  1347.                    nil))))
  1348.         (setq font-lock-keywords 
  1349.               (symbol-value
  1350.                (or (funcall try (get major-mode 'font-lock-keywords))
  1351.                (funcall try (concat major "-font-lock-keywords"))
  1352.                (funcall try (and (string-match "-mode\\'" major)
  1353.                          (concat (substring 
  1354.                               major 0 
  1355.                               (match-beginning 0))
  1356.                              "-font-lock-keywords")))
  1357.                'font-lock-keywords)))))
  1358.  
  1359.       ;; Case fold?
  1360.       (if (>= (length defaults) 3)
  1361.           (setq font-lock-keywords-case-fold-search (nth 2 defaults))
  1362.         ;; older way:
  1363.         ;; look for a property 'font-lock-keywords-case-fold-search on
  1364.         ;; the major-mode symbol.
  1365.         (let* ((nonexist (make-symbol ""))
  1366.            (value (get major-mode 'font-lock-keywords-case-fold-search
  1367.                    nonexist)))
  1368.           (if (not (eq nonexist value))
  1369.           (setq font-lock-keywords-case-fold-search value))))
  1370.  
  1371.       ;; Syntactic?
  1372.       (if (>= (length defaults) 2)
  1373.           (setq font-lock-keywords-only (nth 1 defaults))
  1374.         ;; older way:
  1375.         ;; cleverly examine the syntax table.
  1376.         (font-lock-examine-syntax-table))
  1377.        
  1378.       ;; Syntax table?
  1379.       (if (nth 3 defaults)
  1380.           (let ((slist (nth 3 defaults)))
  1381.         (setq font-lock-syntax-table
  1382.               (copy-syntax-table (syntax-table)))
  1383.         (while slist
  1384.           (modify-syntax-entry (car (car slist)) (cdr (car slist))
  1385.                        font-lock-syntax-table)
  1386.           (setq slist (cdr slist)))))
  1387.  
  1388.       ;; Syntax function?
  1389.       (cond (defaults
  1390.           (setq font-lock-beginning-of-syntax-function
  1391.             (nth 4 defaults)))
  1392.         (t
  1393.          ;; older way:
  1394.          ;; defaults not specified at all, so use `beginning-of-defun'.
  1395.          (setq font-lock-beginning-of-syntax-function
  1396.                'beginning-of-defun)))))
  1397.  
  1398.     (setq font-lock-defaults-computed t)))
  1399.  
  1400.  
  1401. ;;; Initialization of faces.
  1402.  
  1403. (defconst font-lock-face-list
  1404.   '(font-lock-comment-face
  1405.     font-lock-doc-string-face
  1406.     font-lock-string-face
  1407.     font-lock-keyword-face
  1408.     font-lock-function-name-face
  1409.     font-lock-variable-name-face
  1410.     font-lock-type-face
  1411.     font-lock-reference-face
  1412.     font-lock-preprocessor-face))
  1413.  
  1414. (defun font-lock-reset-face (face)
  1415.   "Reset FACE its default state (from the X resource database).
  1416. Returns whether it is indistinguishable from the default face."
  1417.   (reset-face face)
  1418.   (init-face-from-resources face)
  1419.   (face-differs-from-default-p face))
  1420.  
  1421. (defun font-lock-reset-all-faces ()
  1422.   (mapcar 'font-lock-reset-face font-lock-face-list))
  1423.  
  1424. (defun font-lock-add-fonts (tag-list)
  1425.   ;; Underling comments looks terrible on tty's
  1426.   (if (featurep 'tty)
  1427.       (progn
  1428.     (set-face-underline-p 'font-lock-comment-face nil 'global
  1429.                   (append '(tty) tag-list) 'append)
  1430.     (set-face-highlight-p 'font-lock-comment-face t 'global
  1431.                   (append '(tty) tag-list)  'append)))
  1432.   (set-face-font 'font-lock-comment-face [italic] 'global tag-list 'append)
  1433.   (set-face-font 'font-lock-string-face [italic] 'global tag-list 'append)
  1434.   (set-face-font 'font-lock-doc-string-face [italic] 'global tag-list 'append)
  1435.   (set-face-font 'font-lock-function-name-face [bold] 'global tag-list 'append)
  1436.   (set-face-font 'font-lock-variable-name-face [bold] 'global tag-list 'append)
  1437.   (set-face-font 'font-lock-keyword-face [bold] 'global tag-list 'append)
  1438.   (set-face-font 'font-lock-preprocessor-face [bold-italic] 'global tag-list
  1439.           'append)
  1440.   (set-face-font 'font-lock-type-face [italic] 'global tag-list 'append)
  1441.   (set-face-font 'font-lock-reference-face [bold] 'global tag-list 'append)
  1442.   nil)
  1443.  
  1444. (defun font-lock-add-colors (tag-list)
  1445.   (set-face-foreground 'font-lock-comment-face "red" 'global tag-list 'append)
  1446.   ;(set-face-font 'font-lock-comment-face [italic] 'global tag-list 'append)
  1447.   (set-face-foreground 'font-lock-string-face "green4" 'global tag-list
  1448.                'append)
  1449.   (set-face-foreground 'font-lock-string-face "green" 'global tag-list
  1450.                'append)
  1451.   (set-face-foreground 'font-lock-doc-string-face "green4" 'global tag-list
  1452.                'append)
  1453.   (set-face-foreground 'font-lock-doc-string-face "green" 'global tag-list
  1454.                'append)
  1455.   (set-face-foreground 'font-lock-function-name-face "blue3" 'global tag-list
  1456.                'append)
  1457.   (set-face-foreground 'font-lock-function-name-face "blue" 'global tag-list
  1458.                'append)
  1459.   (set-face-foreground 'font-lock-variable-name-face "blue3" 'global tag-list
  1460.                'append)
  1461.   (set-face-foreground 'font-lock-variable-name-face "blue" 'global tag-list
  1462.                'append)
  1463.   (set-face-foreground 'font-lock-reference-face "red3" 'global
  1464.                tag-list 'append)
  1465.   (set-face-foreground 'font-lock-reference-face "red" 'global tag-list
  1466.                'append)
  1467.   (set-face-foreground 'font-lock-keyword-face "orange" 'global tag-list
  1468.                'append)
  1469.   ;(set-face-font 'font-lock-keyword-face [bold] 'global tag-list 'append)
  1470.   (set-face-foreground 'font-lock-preprocessor-face "blue3" 'global tag-list
  1471.                'append)
  1472.   (set-face-foreground 'font-lock-preprocessor-face "blue" 'global tag-list
  1473.                'append)
  1474.   ;(set-face-font 'font-lock-preprocessor-face [bold] 'global tag-list 'append)
  1475.   (set-face-foreground 'font-lock-type-face "#6920ac" 'global tag-list 'append)
  1476.   nil)
  1477.  
  1478. (defun font-lock-apply-defaults (function tag-list)
  1479.   (if (and (listp tag-list)
  1480.        (eq 'or (car tag-list)))
  1481.       (mapcar #'(lambda (x)
  1482.           (font-lock-apply-defaults function x))
  1483.           (cdr tag-list))
  1484.     (if tag-list
  1485.     (if (not (valid-specifier-tag-set-p tag-list))
  1486.         (warn "Invalid tag set found: %s" tag-list)
  1487.       (funcall function tag-list)))))
  1488.  
  1489. (defun font-lock-recompute-variables ()
  1490.   ;; Is this a Draconian thing to do?
  1491.   (mapcar #'(lambda (buffer)
  1492.           (save-excursion
  1493.         (set-buffer buffer)
  1494.         (font-lock-mode 0)
  1495.         (font-lock-set-defaults t)))
  1496.       (buffer-list)))
  1497.  
  1498. ;; Backwards-compatible crud.
  1499.  
  1500. (defun font-lock-use-default-fonts ()
  1501.   "Reset the font-lock faces to a default set of fonts."
  1502.   (interactive)
  1503.   (font-lock-reset-all-faces)
  1504.   (font-lock-add-fonts nil))
  1505.  
  1506. (defun font-lock-use-default-colors ()
  1507.   "Reset the font-lock faces to a default set of colors."
  1508.   (interactive)
  1509.   (font-lock-reset-all-faces)
  1510.   (font-lock-add-colors nil))
  1511.  
  1512. (defun font-lock-use-default-minimal-decoration ()
  1513.   "Reset the font-lock patterns to a fast, minimal set of decorations."
  1514.   (and font-lock-maximum-decoration
  1515.        (setq font-lock-maximum-decoration nil)
  1516.        (font-lock-recompute-variables)))
  1517.  
  1518. (defun font-lock-use-default-maximal-decoration ()
  1519.   "Reset the font-lock patterns to a larger set of decorations."
  1520.   (and (not (eq t font-lock-maximum-decoration))
  1521.        (setq font-lock-maximum-decoration t)
  1522.        (font-lock-recompute-variables)))
  1523.  
  1524.  
  1525. ;;;;;;;;;;;;;;;;;;;;;;         keywords         ;;;;;;;;;;;;;;;;;;;;;;
  1526.  
  1527. ;;; Various major-mode interfaces.
  1528. ;;; Probably these should go in with the source of the respective major modes.
  1529.  
  1530. ;; The defaults and keywords listed here should perhaps be moved into
  1531. ;; mode-specific files.
  1532.  
  1533. ;; For C and Lisp modes we use `beginning-of-defun', rather than nil,
  1534. ;; for SYNTAX-BEGIN.  Thus the calculation of the cache is usually
  1535. ;; faster but not infallible, so we risk mis-fontification.  --sm.
  1536.  
  1537. (put 'c-mode 'font-lock-defaults 
  1538.      '((c-font-lock-keywords
  1539.     c-font-lock-keywords-1 c-font-lock-keywords-2 c-font-lock-keywords-3)
  1540.        nil nil ((?_ . "w")) beginning-of-defun))
  1541. (put 'c++-c-mode 'font-lock-defaults 'c-mode)
  1542. (put 'elec-c-mode 'font-lock-defaults 'c-mode)
  1543.  
  1544. (put 'c++-mode 'font-lock-defaults
  1545.      '((c++-font-lock-keywords
  1546.     c++-font-lock-keywords-1 c++-font-lock-keywords-2
  1547.     c++-font-lock-keywords-3)
  1548.        nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun))
  1549.  
  1550. (put 'java-mode 'font-lock-defaults 
  1551.      '((java-font-lock-keywords
  1552.     java-font-lock-keywords-1 java-font-lock-keywords-2
  1553.     java-font-lock-keywords-3)
  1554.        nil nil ((?_ . "w")) beginning-of-defun
  1555.        (font-lock-mark-block-function . mark-defun)))
  1556.  
  1557. (put 'lisp-mode 'font-lock-defaults
  1558.      '((lisp-font-lock-keywords
  1559.     lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
  1560.        nil nil
  1561.        ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w")
  1562.     (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
  1563.     (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
  1564.        beginning-of-defun))
  1565. (put 'emacs-lisp-mode 'font-lock-defaults 'lisp-mode)
  1566. (put 'lisp-interaction-mode 'font-lock-defaults 'lisp-mode)
  1567.  
  1568. (put 'scheme-mode 'font-lock-defaults
  1569.      '(scheme-font-lock-keywords
  1570.        nil t
  1571.        ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w")
  1572.     (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
  1573.     (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
  1574.        beginning-of-defun))
  1575. (put 'inferior-scheme-mode 'font-lock-defaults 'scheme-mode)
  1576. (put 'scheme-interaction-mode 'font-lock-defaults 'scheme-mode)
  1577.  
  1578. (put 'tex-mode 'font-lock-defaults
  1579.      ;; For TeX modes we could use `backward-paragraph' for the same reason.
  1580.      '(tex-font-lock-keywords nil nil ((?$ . "\""))))
  1581. ;; the nine billion names of TeX mode...
  1582. (put 'bibtex-mode    'font-lock-defaults 'tex-mode)
  1583. (put 'plain-tex-mode    'font-lock-defaults 'tex-mode)
  1584. (put 'slitex-tex-mode    'font-lock-defaults 'tex-mode)
  1585. (put 'SliTeX-mode    'font-lock-defaults 'tex-mode)
  1586. (put 'slitex-mode    'font-lock-defaults 'tex-mode)
  1587. (put 'latex-tex-mode    'font-lock-defaults 'tex-mode)
  1588. (put 'LaTex-tex-mode    'font-lock-defaults 'tex-mode)
  1589. (put 'latex-mode        'font-lock-defaults 'tex-mode)
  1590. (put 'LaTeX-mode        'font-lock-defaults 'tex-mode)
  1591. (put 'japanese-LaTeX-mode 'font-lock-defaults 'tex-mode)
  1592. (put 'japanese-SliTeX-mode 'font-lock-defaults 'tex-mode)
  1593. (put 'FoilTeX-mode    'font-lock-defaults 'tex-mode)
  1594. (put 'LATeX-MoDe    'font-lock-defaults 'tex-mode)
  1595. (put 'lATEx-mODe    'font-lock-defaults 'tex-mode)
  1596. ;; ok, this is getting a bit silly ...
  1597. (put 'eDOm-xETAl    'font-lock-defaults 'tex-mode)
  1598.  
  1599. ;;; Various regexp information shared by several modes.
  1600. ;;; Information specific to a single mode should go in its load library.
  1601.  
  1602. (defconst lisp-font-lock-keywords-1
  1603.   (list
  1604.    ;; Anything not a variable or type declaration is fontified as a function.
  1605.    ;; It would be cleaner to allow preceding whitespace, but it would also be
  1606.    ;; about five times slower.
  1607.    (list (concat "^(\\(def\\("
  1608.           ;; Variable declarations.
  1609.           "\\(const\\(\\|ant\\)\\|ine-key\\(\\|-after\\)\\|var\\)\\|"
  1610.           ;; Structure declarations.
  1611.           "\\(class\\|struct\\|type\\)\\|"
  1612.           ;; Everything else is a function declaration.
  1613.           "\\([^ \t\n\(\)]+\\)"
  1614.           "\\)\\)\\>"
  1615.           ;; Any whitespace and declared object.
  1616.           "[ \t'\(]*"
  1617.           "\\([^ \t\n\)]+\\)?")
  1618.       '(1 font-lock-keyword-face)
  1619.       '(8 (cond ((match-beginning 3) 'font-lock-variable-name-face)
  1620.             ((match-beginning 6) 'font-lock-type-face)
  1621.             (t 'font-lock-function-name-face))
  1622.           nil t))
  1623.    )
  1624.  "Subdued level highlighting Lisp modes.")
  1625.  
  1626. (defconst lisp-font-lock-keywords-2
  1627.   (append lisp-font-lock-keywords-1
  1628.    (list
  1629.     ;;
  1630.     ;; Control structures.  ELisp and CLisp combined.
  1631.     ;;
  1632.     ;;(regexp-opt
  1633.     ;;  '("cond" "if" "while" "let" "let*" "prog" "progn" "prog1"
  1634.     ;;    "prog2" "progv" "catch" "throw" "save-restriction"
  1635.     ;;    "save-excursion" "save-window-excursion"
  1636.     ;;    "save-current-buffer" "with-current-buffer"
  1637.     ;;    "with-temp-file" "with-temp-buffer" "with-output-to-string"
  1638.     ;;    "with-string-as-buffer-contents"
  1639.     ;;    "save-selected-window" "save-match-data" "unwind-protect"
  1640.     ;;    "condition-case" "track-mouse" "autoload"
  1641.     ;;    "eval-after-load" "eval-and-compile" "eval-when-compile"
  1642.     ;;    "when" "unless" "do" "dolist" "dotimes" "flet" "labels"
  1643.     ;;    "lambda" "return" "return-from"))
  1644.     (cons
  1645.      (concat
  1646.       "(\\("
  1647.       "autoload\\|c\\(atch\\|ond\\(ition-case\\)?\\)\\|do\\(list\\|"
  1648.       "times\\)?\\|eval-\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\|"
  1649.       "flet\\|if\\|l\\(a\\(bels\\|mbda\\)\\|et\\*?\\)\\|"
  1650.       "prog[nv12\\*]?\\|return\\(-from\\)?\\|save-\\(current-buffer\\|"
  1651.       "excursion\\|match-data\\|restriction\\|selected-window\\|"
  1652.       "window-excursion\\)\\|t\\(hrow\\|rack-mouse\\)\\|un\\(less\\|"
  1653.       "wind-protect\\)\\|w\\(h\\(en\\|ile\\)\\|ith-\\(current-buffer\\|"
  1654.       "output-to-string\\|string-as-buffer-contents\\|temp-\\(buffer\\|"
  1655.       "file\\)\\)\\)"
  1656.       "\\)\\>") 1)
  1657.     ;;
  1658.     ;; Words inside \\[] tend to be for `substitute-command-keys'.
  1659.     '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
  1660.     ;;
  1661.     ;; Words inside `' tend to be symbol names.
  1662.     '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend)
  1663.     ;;
  1664.     ;; CLisp `:' keywords as references.
  1665.     '("\\<:\\sw+\\>" 0 font-lock-reference-face prepend)
  1666.     ;;
  1667.     ;; ELisp and CLisp `&' keywords as types.
  1668.     '("\\<\\&\\(optional\\|rest\\|whole\\)\\>" . font-lock-type-face)
  1669.     ))
  1670.   "Gaudy level highlighting for Lisp modes.")
  1671.  
  1672. (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
  1673.   "Default expressions to highlight in Lisp modes.")
  1674.  
  1675. ;; The previous version, before replacing it with the FSF version.
  1676. ;(defconst lisp-font-lock-keywords-1 (purecopy
  1677. ; '(;;
  1678. ;   ;; highlight defining forms.  This doesn't work too nicely for
  1679. ;   ;; (defun (setf foo) ...) but it does work for (defvar foo) which
  1680. ;   ;; is more important.
  1681. ;   ("^(def[-a-z]+\\s +\\([^ \t\n\)]+\\)" 1 font-lock-function-name-face)
  1682. ;   ;;
  1683. ;   ;; highlight CL keywords (three clauses seems faster than one)
  1684. ;   ("\\s :\\(\\(\\sw\\|\\s_\\)+\\)\\>" . 1)
  1685. ;   ("(:\\(\\(\\sw\\|\\s_\\)+\\)\\>" . 1)
  1686. ;   ("':\\(\\(\\sw\\|\\s_\\)+\\)\\>" . 1)
  1687. ;   ;;
  1688. ;   ;; this is highlights things like (def* (setf foo) (bar baz)), but may
  1689. ;   ;; be slower (I haven't really thought about it)
  1690. ;;   ("^(def[-a-z]+\\s +\\(\\s(\\S)*\\s)\\|\\S(\\S *\\)"
  1691. ;;    1 font-lock-function-name-face)
  1692. ;   ))
  1693. ; "For consideration as a value of `lisp-font-lock-keywords'.
  1694. ;This does fairly subdued highlighting.")
  1695. ;
  1696. ;(defconst lisp-font-lock-keywords-2 (purecopy
  1697. ;  (append lisp-font-lock-keywords-1
  1698. ;   '(;;
  1699. ;     ;; Highlight control structures
  1700. ;     ("(\\(cond\\|if\\|when\\|unless\\|[ec]?\\(type\\)?case\\)[ \t\n]" . 1)
  1701. ;     ("(\\(while\\|do\\|let\\*?\\|flet\\|labels\\|prog[nv12*]?\\)[ \t\n]" . 1)
  1702. ;     ("(\\(do\\*\\|dotimes\\|dolist\\|loop\\)[ \t\n]" . 1)
  1703. ;     ("(\\(catch\\|\\throw\\|block\\|return\\|return-from\\)[ \t\n]" . 1)
  1704. ;     ("(\\(save-restriction\\|save-window-restriction\\)[ \t\n]" . 1)
  1705. ;     ("(\\(save-excursion\\|unwind-protect\\|condition-case\\)[ \t\n]" . 1)
  1706. ;     ;;
  1707. ;     ;; highlight function names in emacs-lisp docstrings (in the syntax
  1708. ;     ;; that substitute-command-keys understands.)
  1709. ;     ("\\\\\\\\\\[\\([^]\\\n]+\\)]" 1 font-lock-keyword-face t)
  1710. ;     ;;
  1711. ;     ;; highlight words inside `' which tend to be function names
  1712. ;     ("`\\([-a-zA-Z0-9_][-a-zA-Z0-9_][-a-zA-Z0-9_.]+\\)'"
  1713. ;      1 font-lock-keyword-face t)
  1714. ;     )))
  1715. ; "For consideration as a value of `lisp-font-lock-keywords'.
  1716. ;
  1717. ;This does a lot more highlighting.")
  1718.  
  1719. (defvar scheme-font-lock-keywords
  1720.   (eval-when-compile
  1721.     (list
  1722.      ;;
  1723.      ;; Declarations.  Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
  1724.      ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
  1725.      (list (concat "(\\(define\\("
  1726.            ;; Function names.
  1727.            "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|"
  1728.            ;; Macro names, as variable names.  A bit dubious, this.
  1729.            "\\(-syntax\\)\\|"
  1730.            ;; Class names.
  1731.            "\\(-class\\)"
  1732.            "\\)\\)\\>"
  1733.            ;; Any whitespace and declared object.
  1734.            "[ \t]*(?"
  1735.            "\\(\\sw+\\)?")
  1736.        '(1 font-lock-keyword-face)
  1737.        '(8 (cond ((match-beginning 3) 'font-lock-function-name-face)
  1738.              ((match-beginning 6) 'font-lock-variable-name-face)
  1739.              (t 'font-lock-type-face))
  1740.            nil t))
  1741.      ;;
  1742.      ;; Control structures.
  1743. ;(regexp-opt '("begin" "call-with-current-continuation" "call/cc"
  1744. ;           "call-with-input-file" "call-with-output-file" "case" "cond"
  1745. ;           "do" "else" "for-each" "if" "lambda"
  1746. ;           "let\\*?" "let-syntax" "letrec" "letrec-syntax"
  1747. ;           ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
  1748. ;           "and" "or" "delay"
  1749. ;           ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
  1750. ;           ;;"quasiquote" "quote" "unquote" "unquote-splicing"
  1751. ;           "map" "syntax" "syntax-rules"))
  1752.      (cons
  1753.       (concat "(\\("
  1754.           "and\\|begin\\|c\\(a\\(ll\\(-with-\\(current-continuation\\|"
  1755.           "input-file\\|output-file\\)\\|/cc\\)\\|se\\)\\|ond\\)\\|"
  1756.           "d\\(elay\\|o\\)\\|else\\|for-each\\|if\\|"
  1757.           "l\\(ambda\\|et\\(-syntax\\|\\*?\\|rec\\(\\|-syntax\\)\\)\\)\\|"
  1758.           "map\\|or\\|syntax\\(\\|-rules\\)"
  1759.           "\\)\\>") 1)
  1760.      ;;
  1761.      ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
  1762.      '("\\<<\\sw+>\\>" . font-lock-type-face)
  1763.      ;;
  1764.      ;; Scheme `:' keywords as references.
  1765.      '("\\<:\\sw+\\>" . font-lock-reference-face)
  1766.      ))
  1767. "Default expressions to highlight in Scheme modes.")
  1768.  
  1769. ;; The previous version, before replacing it with the FSF version.
  1770. ;(defconst scheme-font-lock-keywords (purecopy
  1771. ; '(("(define[ \t]+(?\\([^ \t\n\)]+\\)" 1 font-lock-function-name-face)
  1772. ;   ("(\\(cond\\|lambda\\|begin\\|if\\|else\\|case\\|do\\)[ \t\n]" . 1)
  1773. ;   ("(\\(\\|letrec\\|let\\*?\\|set!\\|and\\|or\\)[ \t\n]" . 1)
  1774. ;   ("(\\(quote\\|unquote\\|quasiquote\\|unquote-splicing\\)[ \t\n]" . 1)
  1775. ;   ("(\\(syntax\\|syntax-rules\\|define-syntax\\|let-syntax\\|letrec-syntax\\)[ \t\n]" . 1)))
  1776. ;  "Expressions to highlight in Scheme buffers.")
  1777.  
  1778. (defconst c-font-lock-keywords-1 nil
  1779.   "Subdued level highlighting for C modes.")
  1780.  
  1781. (defconst c-font-lock-keywords-2 nil
  1782.   "Medium level highlighting for C modes.")
  1783.  
  1784. (defconst c-font-lock-keywords-3 nil
  1785.   "Gaudy level highlighting for C modes.")
  1786.  
  1787. (defconst c++-font-lock-keywords-1 nil
  1788.   "Subdued level highlighting for C++ modes.")
  1789.  
  1790. (defconst c++-font-lock-keywords-2 nil
  1791.   "Medium level highlighting for C++ modes.")
  1792.  
  1793. (defconst c++-font-lock-keywords-3 nil
  1794.   "Gaudy level highlighting for C++ modes.")
  1795.  
  1796. (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
  1797.   ;; Match, and move over, any declaration/definition item after point.
  1798.   ;; The expect syntax of an item is "word" or "word::word", possibly ending
  1799.   ;; with optional whitespace and a "(".  Everything following the item (but
  1800.   ;; belonging to it) is expected to by skip-able by `forward-sexp', and items
  1801.   ;; are expected to be separated with a "," or ";".
  1802.   (if (looking-at "[ \t*&]*\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*\\((\\)?")
  1803.       (save-match-data
  1804.     (condition-case nil
  1805.         (save-restriction
  1806.           ;; Restrict to the end of line, currently guaranteed to be LIMIT.
  1807.           (narrow-to-region (point-min) limit)
  1808.           (goto-char (match-end 1))
  1809.           ;; Move over any item value, etc., to the next item.
  1810.           (while (not (looking-at "[ \t]*\\([,;]\\|$\\)"))
  1811.         (goto-char (or (scan-sexps (point) 1) (point-max))))
  1812.           (goto-char (match-end 0)))
  1813.       (error t)))))
  1814.  
  1815. (let ((c-keywords
  1816. ;      ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
  1817.        "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
  1818.       (c-type-types
  1819. ;      ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
  1820. ;    "signed" "unsigned" "short" "long" "int" "char" "float" "double"
  1821. ;    "void" "volatile" "const")
  1822.        (concat "auto\\|c\\(har\\|onst\\)\\|double\\|e\\(num\\|xtern\\)\\|"
  1823.            "float\\|int\\|long\\|register\\|"
  1824.            "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
  1825.            "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)"))    ; 6 ()s deep.
  1826.       (c++-keywords
  1827. ;      ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
  1828. ;    "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
  1829. ;       "protected" "private" "public")
  1830.        (concat "asm\\|break\\|c\\(atch\\|ontinue\\)\\|d\\(elete\\|o\\)\\|"
  1831.            "else\\|for\\|if\\|new\\|"
  1832.            "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|return\\|"
  1833.            "s\\(izeof\\|witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while"))
  1834.       (c++-type-types
  1835. ;      ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
  1836. ;    "signed" "unsigned" "short" "long" "int" "char" "float" "double"
  1837. ;    "void" "volatile" "const" "class" "inline" "friend" "bool"
  1838. ;    "virtual" "complex" "template")
  1839.        (concat "auto\\|bool\\|c\\(har\\|lass\\|o\\(mplex\\|nst\\)\\)\\|"
  1840.            "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|"
  1841.            "in\\(line\\|t\\)\\|long\\|register\\|"
  1842.            "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
  1843.            "t\\(emplate\\|ypedef\\)\\|un\\(ion\\|signed\\)\\|"
  1844.            "v\\(irtual\\|o\\(id\\|latile\\)\\)"))        ; 11 ()s deep.
  1845.       (ctoken "\\(\\sw\\|\\s_\\|[:~*&]\\)+")
  1846.       )
  1847.  (setq c-font-lock-keywords-1
  1848.   (list
  1849.    ;;
  1850.    ;; These are all anchored at the beginning of line for speed.
  1851.    ;;
  1852.    ;; Fontify function name definitions (GNU style; without type on line).
  1853.    
  1854.    ;; In FSF this has the simpler definition of "\\sw+" for ctoken.
  1855.    ;; I'm not sure if ours is more correct.
  1856.    ;; This is a subset of the next rule, and is slower when present. --dmoore
  1857.    ;; (list (concat "^\\(" ctoken "\\)[ \t]*(") 1 'font-lock-function-name-face)
  1858.    ;;
  1859.    ;; fontify the names of functions being defined.
  1860.    ;; FSF doesn't have this but I think it should be fast for us because
  1861.    ;; our regexp routines are more intelligent than FSF's about handling
  1862.    ;; anchored-at-newline. (When I added this hack in regex.c, it halved
  1863.    ;; the time to do the regexp phase of font-lock for a C file!) Not
  1864.    ;; including this discriminates against those who don't follow the
  1865.    ;; GNU coding style. --ben
  1866.    ;; x?x?x?y?z should always be: (x(xx?)?)?y?z --dmoore
  1867.    (list (concat
  1868.       "^\\("
  1869.           "\\(" ctoken "[ \t]+\\)"    ; type specs; there can be no
  1870.       "\\("
  1871.           "\\(" ctoken "[ \t]+\\)"    ; more than 3 tokens, right?
  1872.           "\\(" ctoken "[ \t]+\\)"
  1873.       "?\\)?\\)?"
  1874.           "\\([*&]+[ \t]*\\)?"        ; pointer
  1875.           "\\(" ctoken "\\)[ \t]*(")    ; name
  1876.          10 'font-lock-function-name-face)
  1877.    ;;
  1878.    ;; This is faster but not by much.  I don't see why not.
  1879.    ;(list (concat "^\\(" ctoken "\\)[ \t]*(") 1 'font-lock-function-name-face)
  1880.    ;;
  1881.    ;; Added next two; they're both jolly-good fastmatch candidates so
  1882.    ;; should be fast. --ben
  1883.    ;;
  1884.    ;; Fontify structure names (in structure definition form).
  1885.    (list (concat "^\\(typedef[ \t]+struct\\|struct\\|static[ \t]+struct\\)"
  1886.              "[ \t]+\\(" ctoken "\\)[ \t]*\\(\{\\|$\\)")
  1887.          2 'font-lock-function-name-face)
  1888.    ;;
  1889.    ;; Fontify case clauses.  This is fast because its anchored on the left.
  1890.    '("case[ \t]+\\(\\(\\sw\\|\\s_\\)+\\)[ \t]+:". 1)
  1891.    ;;
  1892.    '("\\<\\(default\\):". 1)
  1893.    ;; Fontify filenames in #include <...> preprocessor directives as strings.
  1894.    '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
  1895.    ;;
  1896.    ;; Fontify function macro names.
  1897.    '("^#[ \t]*define[ \t]+\\(\\(\\sw+\\)(\\)" 2 font-lock-function-name-face)
  1898.    ;;
  1899.    ;; Fontify symbol names in #if ... defined preprocessor directives.
  1900.    '("^#[ \t]*if\\>"
  1901.      ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
  1902.       (1 font-lock-preprocessor-face) (2 font-lock-variable-name-face nil t)))
  1903.    ;;
  1904.    ;; Fontify symbol names in #elif ... defined preprocessor directives.
  1905.    '("^#[ \t]*elif\\>"
  1906.      ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
  1907.       (1 font-lock-preprocessor-face) (2 font-lock-variable-name-face nil t)))
  1908.    ;;
  1909.    ;; Fontify otherwise as symbol names, and the preprocessor directive names.
  1910.    '("^\\(#[ \t]*[a-z]+\\)\\>[ \t]*\\(\\sw+\\)?"
  1911.      (1 font-lock-preprocessor-face) (2 font-lock-variable-name-face nil t))
  1912.    ))
  1913.  
  1914.  (setq c-font-lock-keywords-2
  1915.   (append c-font-lock-keywords-1
  1916.    (list
  1917.     ;;
  1918.     ;; Simple regexps for speed.
  1919.     ;;
  1920.     ;; Fontify all type specifiers.
  1921.     (cons (concat "\\<\\(" c-type-types "\\)\\>") 'font-lock-type-face)
  1922.     ;;
  1923.     ;; Fontify all builtin keywords (except case, default and goto; see below).
  1924.     (cons (concat "\\<\\(" c-keywords "\\)\\>") 'font-lock-keyword-face)
  1925.     ;;
  1926.     ;; Fontify case/goto keywords and targets, and case default/goto tags.
  1927.     '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?"
  1928.       (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
  1929.     '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face)
  1930.     )))
  1931.  
  1932.  (setq c-font-lock-keywords-3
  1933.   (append c-font-lock-keywords-2
  1934.    ;;
  1935.    ;; More complicated regexps for more complete highlighting for types.
  1936.    ;; We still have to fontify type specifiers individually, as C is so hairy.
  1937.    (list
  1938.     ;;
  1939.     ;; Fontify all storage classes and type specifiers, plus their items.
  1940.     (list (concat "\\<\\(" c-type-types "\\)\\>"
  1941.           "\\([ \t*&]+\\sw+\\>\\)*")
  1942.       ;; Fontify each declaration item.
  1943.       '(font-lock-match-c++-style-declaration-item-and-skip-to-next
  1944.         ;; Start with point after all type specifiers.
  1945.         (goto-char (or (match-beginning 8) (match-end 1)))
  1946.         ;; Finish with point after first type specifier.
  1947.         (goto-char (match-end 1))
  1948.         ;; Fontify as a variable or function name.
  1949.         (1 (if (match-beginning 4)
  1950.            font-lock-function-name-face
  1951.          font-lock-variable-name-face))))
  1952.     ;;
  1953.     ;; Fontify structures, or typedef names, plus their items.
  1954.     '("\\(}\\)[ \t*]*\\sw"
  1955.       (font-lock-match-c++-style-declaration-item-and-skip-to-next
  1956.        (goto-char (match-end 1)) nil
  1957.        (1 (if (match-beginning 4)
  1958.           font-lock-function-name-face
  1959.         font-lock-variable-name-face))))
  1960.     ;;
  1961.     ;; Fontify anything at beginning of line as a declaration or definition.
  1962.     '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
  1963.       (1 font-lock-type-face)
  1964.       (font-lock-match-c++-style-declaration-item-and-skip-to-next
  1965.        (goto-char (or (match-beginning 2) (match-end 1))) nil
  1966.        (1 (if (match-beginning 4)
  1967.           font-lock-function-name-face
  1968.         font-lock-variable-name-face))))
  1969.     )))
  1970.  
  1971.  (setq c++-font-lock-keywords-1
  1972.   (append
  1973.    ;;
  1974.    ;; The list `c-font-lock-keywords-1' less that for function names.
  1975.    ;; the simple function form regexp has been removed. --dmoore
  1976.    ;;(cdr c-font-lock-keywords-1)
  1977.    c-font-lock-keywords-1
  1978.    ;;
  1979.    ;; Fontify function name definitions, possibly incorporating class name.
  1980.    (list
  1981.     '("^\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*("
  1982.       (1 (if (match-beginning 2)
  1983.          font-lock-type-face
  1984.        font-lock-function-name-face))
  1985.       (3 (if (match-beginning 2) font-lock-function-name-face) nil t))
  1986.     )))
  1987.  
  1988.  (setq c++-font-lock-keywords-2
  1989.   (append c++-font-lock-keywords-1
  1990.    (list
  1991.     ;;
  1992.     ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
  1993.     (cons (concat "\\<\\(" c++-type-types "\\)\\>") 'font-lock-type-face)
  1994.     ;;
  1995.     ;; Fontify operator function name overloading.
  1996.     '("\\<\\(operator\\)\\>[ \t]*\\([][)(><!=+-][][)(><!=+-]?\\)?"
  1997.       (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
  1998.     ;;
  1999.     ;; Fontify case/goto keywords and targets, and case default/goto tags.
  2000.     '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?"
  2001.       (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
  2002.     '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face)
  2003.     ;;
  2004.     ;; Fontify other builtin keywords.
  2005.     (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face)
  2006.     )))
  2007.  
  2008.  (setq c++-font-lock-keywords-3
  2009.   (append c++-font-lock-keywords-2
  2010.    ;;
  2011.    ;; More complicated regexps for more complete highlighting for types.
  2012.    (list
  2013.     ;;
  2014.     ;; Fontify all storage classes and type specifiers, plus their items.
  2015.     (list (concat "\\<\\(" c++-type-types "\\)\\>"
  2016.           "\\([ \t*&]+\\sw+\\>\\)*")
  2017.       ;; Fontify each declaration item.
  2018.       '(font-lock-match-c++-style-declaration-item-and-skip-to-next
  2019.         ;; Start with point after all type specifiers.
  2020.         (goto-char (or (match-beginning 13) (match-end 1)))
  2021.         ;; Finish with point after first type specifier.
  2022.         (goto-char (match-end 1))
  2023.         ;; Fontify as a variable or function name.
  2024.         (1 (cond ((match-beginning 2) 'font-lock-type-face)
  2025.              ((match-beginning 4) 'font-lock-function-name-face)
  2026.              (t 'font-lock-variable-name-face)))
  2027.         (3 (if (match-beginning 4)
  2028.            'font-lock-function-name-face
  2029.          'font-lock-variable-name-face) nil t)))
  2030.     ;;
  2031.     ;; Fontify structures, or typedef names, plus their items.
  2032.     '("\\(}\\)[ \t*]*\\sw"
  2033.       (font-lock-match-c++-style-declaration-item-and-skip-to-next
  2034.        (goto-char (match-end 1)) nil
  2035.        (1 (if (match-beginning 4)
  2036.           font-lock-function-name-face
  2037.         font-lock-variable-name-face))))
  2038.     ;;
  2039.     ;; Fontify anything at beginning of line as a declaration or definition.
  2040.     '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
  2041.       (1 font-lock-type-face)
  2042.       (font-lock-match-c++-style-declaration-item-and-skip-to-next
  2043.        (goto-char (or (match-beginning 2) (match-end 1))) nil
  2044.        (1 (cond ((match-beginning 2) 'font-lock-type-face)
  2045.         ((match-beginning 4) 'font-lock-function-name-face)
  2046.         (t 'font-lock-variable-name-face)))
  2047.        (3 (if (match-beginning 4)
  2048.           'font-lock-function-name-face
  2049.         'font-lock-variable-name-face) nil t)))
  2050.     )))
  2051.  )
  2052.  
  2053. (defvar c-font-lock-keywords c-font-lock-keywords-1
  2054.   "Default expressions to highlight in C mode.")
  2055.  
  2056. (defvar c++-font-lock-keywords c++-font-lock-keywords-1
  2057.   "Default expressions to highlight in C++ mode.")
  2058.  
  2059. ;; The previous version, before replacing it with the FSF version.
  2060. ;(defconst c-font-lock-keywords-1 nil
  2061. ; "For consideration as a value of `c-font-lock-keywords'.
  2062. ;This does fairly subdued highlighting.")
  2063. ;
  2064. ;(defconst c-font-lock-keywords-2 nil
  2065. ; "For consideration as a value of `c-font-lock-keywords'.
  2066. ;This does a lot more highlighting.")
  2067. ;
  2068. ;(let ((storage "auto\\|extern\\|register\\|static\\|volatile")
  2069. ;      (prefixes "unsigned\\|short\\|long\\|const")
  2070. ;      (types (concat "int\\|long\\|char\\|float\\|double\\|void\\|struct\\|"
  2071. ;              "union\\|enum\\|typedef"))
  2072. ;      (ctoken "\\(\\sw\\|\\s_\\|[:~*&]\\)+")
  2073. ;      )
  2074. ;  (setq c-font-lock-keywords-1 (purecopy
  2075. ;   (list
  2076. ;    ;; fontify preprocessor directives.
  2077. ;    '("^#[ \t]*[a-z]+" . font-lock-preprocessor-face)
  2078. ;    ;;
  2079. ;    ;; fontify names being defined.
  2080. ;    '("^#[ \t]*\\(define\\|undef\\)[ \t]+\\(\\(\\sw\\|\\s_\\)+\\)" 2
  2081. ;      font-lock-function-name-face)
  2082. ;    ;;
  2083. ;    ;; fontify other preprocessor lines.
  2084. ;    '("^#[ \t]*\\(if\\|ifn?def\\|elif\\)[ \t]+\\([^\n]+\\)"
  2085. ;      2 font-lock-function-name-face t)
  2086. ;    ;;
  2087. ;    ;; fontify the filename in #include <...>
  2088. ;    ;; don't need to do this for #include "..." because those were
  2089. ;    ;; already fontified as strings by the syntactic pass.
  2090. ;    ;; (Changed to not include the <> in the face, since "" aren't.)
  2091. ;    '("^#[ \t]*include[ \t]+<\\([^>\"\n]+\\)>" 1 font-lock-string-face)
  2092. ;    ;;
  2093. ;    ;; fontify the names of functions being defined.
  2094. ;    ;; I think this should be fast because it's anchored at bol, but it's not.
  2095. ;    (list (concat
  2096. ;        "^\\(" ctoken "[ \t]+\\)?"    ; type specs; there can be no
  2097. ;        "\\(" ctoken "[ \t]+\\)?"    ; more than 3 tokens, right?
  2098. ;        "\\(" ctoken "[ \t]+\\)?"
  2099. ;        "\\([*&]+[ \t]*\\)?"        ; pointer
  2100. ;        "\\(" ctoken "\\)[ \t]*(")    ; name
  2101. ;       8 'font-lock-function-name-face)
  2102. ;    ;;
  2103. ;    ;; This is faster but not by much.  I don't see why not.
  2104. ;;    (list (concat "^\\(" ctoken "\\)[ \t]*(") 1 'font-lock-function-name-face)
  2105. ;    ;;
  2106. ;    ;; Fontify structure names (in structure definition form).
  2107. ;    (list (concat "^\\(typedef[ \t]+struct\\|struct\\|static[ \t]+struct\\)"
  2108. ;           "[ \t]+\\(" ctoken "\\)[ \t]*\\(\{\\|$\\)")
  2109. ;       2 'font-lock-function-name-face)
  2110. ;    ;;
  2111. ;    ;; Fontify case clauses.  This is fast because its anchored on the left.
  2112. ;    '("case[ \t]+\\(\\(\\sw\\|\\s_\\)+\\):". 1)
  2113. ;    '("\\<\\(default\\):". 1)
  2114. ;    )))
  2115. ;
  2116. ;  (setq c-font-lock-keywords-2 (purecopy
  2117. ;   (append c-font-lock-keywords-1
  2118. ;    (list
  2119. ;     ;;
  2120. ;     ;; fontify all storage classes and type specifiers
  2121. ;     ;; types should be surrounded by non alphanumerics (Raymond Toy)
  2122. ;     (cons (concat "\\<\\(" storage "\\)\\>") 'font-lock-type-face)
  2123. ;     (list (concat "\\([^a-zA-Z0-9_]\\|^\\)\\("
  2124. ;            types
  2125. ;            "\\)\\([^a-zA-Z0-9_]\\|$\\)")
  2126. ;        2 'font-lock-type-face)
  2127. ;     ;; fontify the prefixes now.  The types should have been fontified
  2128. ;     ;; previously.
  2129. ;     (list (concat "\\<\\(" prefixes "\\)[ \t]+\\(" types "\\)\\>")
  2130. ;        1 'font-lock-type-face)
  2131. ;     ;;
  2132. ;     ;; fontify all builtin tokens
  2133. ;     (cons (concat
  2134. ;         "[ \t]\\("
  2135. ;         (mapconcat 'identity
  2136. ;          '("for" "while" "do" "return" "goto" "case" "break" "switch"
  2137. ;        "if" "then" "else if" "else" "return" "continue" "default"
  2138. ;        )
  2139. ;          "\\|")
  2140. ;         "\\)[ \t\n(){};,]")
  2141. ;        1)
  2142. ;     ;;
  2143. ;     ;; fontify case targets and goto-tags.  This is slow because the
  2144. ;     ;; expression is anchored on the right.
  2145. ;     "\\(\\(\\sw\\|\\s_\\)+\\):"
  2146. ;     ;;
  2147. ;     ;; Fontify variables declared with structures, or typedef names.
  2148. ;     '("}[ \t*]*\\(\\(\\sw\\|\\s_\\)+\\)[ \t]*[,;]"
  2149. ;    1 font-lock-function-name-face)
  2150. ;     ;;
  2151. ;     ;; Fontify global variables without a type.
  2152. ;;     '("^\\([_a-zA-Z0-9:~*]+\\)[ \t]*[[;={]" 1 font-lock-function-name-face)
  2153. ;
  2154. ;     ))))
  2155. ;  )
  2156. ;
  2157. ;
  2158. ;;; default to the gaudier variety?
  2159. ;;(defconst c-font-lock-keywords c-font-lock-keywords-2
  2160. ;;  "Additional expressions to highlight in C mode.")
  2161. ;(defconst c-font-lock-keywords c-font-lock-keywords-1
  2162. ;  "Additional expressions to highlight in C mode.")
  2163. ;
  2164. ;(defconst c++-font-lock-keywords-1 nil
  2165. ; "For consideration as a value of `c++-font-lock-keywords'.
  2166. ;This does fairly subdued highlighting.")
  2167. ;
  2168. ;(defconst c++-font-lock-keywords-2 nil
  2169. ; "For consideration as a value of `c++-font-lock-keywords'.
  2170. ;This does a lot more highlighting.")
  2171. ;
  2172. ;(let ((ctoken "\\(\\sw\\|\\s_\\|[:~*&]\\)+")
  2173. ;      (c++-types (concat "complex\\|public\\|private\\|protected\\|virtual\\|"
  2174. ;              "friend\\|inline"))
  2175. ;      c++-font-lock-keywords-internal-1
  2176. ;      c++-font-lock-keywords-internal-2
  2177. ;      )
  2178. ;  (setq c++-font-lock-keywords-internal-1 (purecopy
  2179. ;   (list
  2180. ;    ;;
  2181. ;    ;; fontify friend operator functions
  2182. ;    '("^\\(operator[^(]*\\)(" 1 font-lock-function-name-face)
  2183. ;    '("^\\(operator[ \\t]*([ \\t]*)[^(]*\\)(" 1 font-lock-function-name-face)
  2184. ;
  2185. ;    ;; fontify the class names only in the definition
  2186. ;    (list (concat "^class[ \t]+" ctoken "[ \t\n{: ;]") 1
  2187. ;       'font-lock-function-name-face)
  2188. ;
  2189. ;    (list (concat
  2190. ;        "^\\(" ctoken "[ \t]+\\)?" ; type specs; there can be no
  2191. ;        "\\(" ctoken "[ \t]+\\)?" ; more than 3 tokens, right?
  2192. ;        "\\(" ctoken "[ \t]+\\)?"
  2193. ;        "\\(\\*+[ \t]*\\)?"    ; pointer
  2194. ;        "\\(" ctoken "\\(::\\)?~?\\(\\(operator[ \t]*[^ \ta-zA-Z]+\\)\\|"
  2195. ;        ctoken "\\)\\)[ \t]*(") ; name
  2196. ;       8 'font-lock-function-name-face t)
  2197. ;    )))
  2198. ;
  2199. ;  (setq c++-font-lock-keywords-internal-2 (purecopy
  2200. ;   (list
  2201. ;    ;; fontify extra c++ storage classes and type specifiers
  2202. ;    (cons (concat "\\<\\(" c++-types "\\)\\>") 'font-lock-type-face)
  2203. ;
  2204. ;    ;;special check for class
  2205. ;    '("^\\(\\<\\|template[ \t]+<[ \t]*\\)\\(class\\)[ \t\n]+" 2
  2206. ;      font-lock-type-face)
  2207. ;
  2208. ;    ;; special handling of template
  2209. ;    "^\\(template\\)\\>"
  2210. ;    ;; fontify extra c++ builtin tokens
  2211. ;    (cons (concat
  2212. ;        "[ \t]\\("
  2213. ;        (mapconcat 'identity
  2214. ;               '("asm" "catch" "throw" "try" "delete" "new" "operator"
  2215. ;             "sizeof" "this"
  2216. ;             )
  2217. ;               "\\|")
  2218. ;        "\\)[ \t\n(){};,]")
  2219. ;       1)
  2220. ;    )))
  2221. ;
  2222. ;  (setq c++-font-lock-keywords-1 (purecopy
  2223. ;   (append c-font-lock-keywords-1 c++-font-lock-keywords-internal-1)))
  2224. ;
  2225. ;  (setq c++-font-lock-keywords-2 (purecopy
  2226. ;   (append c-font-lock-keywords-2 c++-font-lock-keywords-internal-1
  2227. ;        c++-font-lock-keywords-internal-2)))
  2228. ;  )
  2229. ;
  2230. ;(defconst c++-font-lock-keywords c++-font-lock-keywords-1
  2231. ;  "Additional expressions to highlight in C++ mode.")
  2232.  
  2233. ;; Java support from Anders Lindgren and Bob Weiner
  2234.  
  2235. (defconst java-font-lock-keywords-1 nil
  2236.  "For consideration as a value of `java-font-lock-keywords'.
  2237. This does fairly subdued highlighting.")
  2238.  
  2239. (defconst java-font-lock-keywords-2 nil
  2240.  "For consideration as a value of `java-font-lock-keywords'.
  2241. This adds highlighting of types and identifier names.")
  2242.  
  2243. (defconst java-font-lock-keywords-3 nil
  2244.  "For consideration as a value of `java-font-lock-keywords'.
  2245. This adds highlighting of Java documentation tags, such as @see.")
  2246.  
  2247. (defvar java-font-lock-type-regexp
  2248.   (concat "\\<\\(boolean\\|byte\\|char\\|double\\|float\\|int"
  2249.          "\\|long\\|short\\|void\\)\\>")
  2250.   "Regexp which should match a primitive type.")
  2251.  
  2252. (let ((capital-letter "A-Z\300-\326\330-\337")
  2253.       (letter "a-zA-Z_$\300-\326\330-\366\370-\377")
  2254.       (digit  "0-9"))
  2255. (defvar java-font-lock-identifier-regexp
  2256.   (concat "\\<\\([" letter "][" letter digit "]*\\)\\>")
  2257.   "Regexp which should match all Java identifiers.")
  2258.  
  2259. (defvar java-font-lock-class-name-regexp
  2260.   (concat "\\<\\([" capital-letter "][" letter digit "]*\\)\\>")
  2261.   "Regexp which should match a class or an interface name.
  2262. The name is assumed to begin with a capital letter.")
  2263. )
  2264.  
  2265.  
  2266. (let ((java-modifier-regexp
  2267.        (concat "\\<\\(abstract\\|const\\|final\\|native\\|"
  2268.            "private\\|protected\\|public\\|"
  2269.            "static\\|synchronized\\|transient\\|volatile\\)\\>")))
  2270.  
  2271.   ;; Basic font-lock support:
  2272.   (setq java-font-lock-keywords-1
  2273.     (list
  2274.      ;; Keywords:
  2275.      (list        
  2276.       (concat
  2277.        "\\<\\("
  2278.        "break\\|byvalue\\|"
  2279.        "case\\|cast\\|catch\\|class\\|continue\\|"
  2280.        "do\\|else\\|extends\\|"
  2281.        "finally\\|for\\|future\\|"
  2282.        "generic\\|goto\\|"
  2283.        "if\\|implements\\|import\\|"
  2284.        "instanceof\\|interface\\|"
  2285.        "new\\|package\\|return\\|switch\\|"
  2286.        "throws?\\|try\\|while\\)\\>")
  2287.       1 'font-lock-keyword-face)
  2288.  
  2289.      ;; Modifiers:
  2290.      (list java-modifier-regexp 1 font-lock-type-face)
  2291.  
  2292.      ;; Special constants:
  2293.      '("\\<\\(this\\|super\\)\\>" (1 font-lock-reference-face))
  2294.      '("\\<\\(false\\|null\\|true\\)\\>" (1 font-lock-keyword-face))
  2295.  
  2296.      ;; Class names:
  2297.      (list (concat "\\<class\\>\\s *" java-font-lock-identifier-regexp)
  2298.            1 'font-lock-function-name-face)
  2299.         
  2300.      ;; Package declarations:
  2301.      (list (concat "\\<\\(package\\|import\\)\\>\\s *"
  2302.                java-font-lock-identifier-regexp)
  2303.            '(2 font-lock-reference-face)
  2304.            (list (concat
  2305.               "\\=\\.\\(" java-font-lock-identifier-regexp "\\)")
  2306.              nil nil '(1 (if (eq (char-after (match-end 0)) ?.)
  2307.                      'font-lock-reference-face
  2308.                    'font-lock-type-face))))
  2309.      
  2310.      ;; Constructors:
  2311.      (list (concat
  2312.         "^\\s *\\(" java-modifier-regexp "\\s +\\)*"
  2313.         java-font-lock-class-name-regexp "\\s *\(")
  2314.            (list 3
  2315.              '(condition-case nil
  2316.               (save-excursion
  2317.                 (goto-char (scan-sexps (- (match-end 0) 1) 1))
  2318.                 (parse-partial-sexp (point) (point-max) nil t)
  2319.                 (and (looking-at "\\($\\|\\<throws\\>\\|{\\)")
  2320.                  'font-lock-function-name-face))
  2321.             (error 'font-lock-function-name-face))))
  2322.  
  2323.      ;; Methods:
  2324.      (list (concat "\\(" java-font-lock-type-regexp "\\|"
  2325.                java-font-lock-class-name-regexp "\\)"
  2326.                "\\s *\\(\\[\\s *\\]\\s *\\)*"
  2327.                java-font-lock-identifier-regexp "\\s *\(")
  2328.            5
  2329.            'font-lock-function-name-face)
  2330.  
  2331.      ;; Labels:
  2332.      (list ":"
  2333.            (list
  2334.         (concat "^\\s *" java-font-lock-identifier-regexp "\\s *:")
  2335.         '(beginning-of-line) '(end-of-line)
  2336.         '(1 font-lock-reference-face)))
  2337.  
  2338.      ;; `break' and continue' destination labels:
  2339.      (list (concat "\\<\\(break\\|continue\\)\\>\\s *"
  2340.                java-font-lock-identifier-regexp)
  2341.            2 'font-lock-reference-face)
  2342.  
  2343.      ;; Case statements:
  2344.      ;; In Java, any constant expression is allowed.
  2345.      '("\\<case\\>\\s *\\(.*\\):" 1 font-lock-reference-face)))
  2346.  
  2347.   ;; Types and declared variable names:
  2348.   (setq java-font-lock-keywords-2
  2349.     (append 
  2350.  
  2351.      java-font-lock-keywords-1
  2352.      (list
  2353.       ;; Keywords followed by a type:
  2354.       (list (concat "\\<\\(extends\\|instanceof\\|new\\)\\>\\s *"
  2355.             java-font-lock-identifier-regexp)
  2356.         '(2 (if (eq (char-after (match-end 0)) ?.)
  2357.             'font-lock-reference-face 'font-lock-type-face))
  2358.         (list (concat "\\=\\." java-font-lock-identifier-regexp)
  2359.               '(goto-char (match-end 0)) nil
  2360.               '(1 (if (eq (char-after (match-end 0)) ?.)
  2361.                   'font-lock-reference-face 'font-lock-type-face))))
  2362.  
  2363.       ;; Keywords followed by a type list:
  2364.       (list (concat "\\<\\(implements\\|throws\\)\\>\\ s*"
  2365.             java-font-lock-identifier-regexp)
  2366.         '(2 (if (eq (char-after (match-end 0)) ?.)
  2367.             font-lock-reference-face font-lock-type-face))
  2368.         (list (concat "\\=\\(\\.\\|\\s *\\(,\\)\\s *\\)"
  2369.                   java-font-lock-identifier-regexp)
  2370.               '(goto-char (match-end 0)) nil
  2371.               '(3 (if (eq (char-after (match-end 0)) ?.)
  2372.                   font-lock-reference-face font-lock-type-face))))
  2373.  
  2374.       ;; primitive types, can't be confused with anything else.
  2375.       (list java-font-lock-type-regexp
  2376.         '(1 font-lock-type-face)
  2377.         '(font-lock-match-java-declarations
  2378.           (goto-char (match-end 0))
  2379.           (goto-char (match-end 0))
  2380.           (0 font-lock-variable-name-face)))
  2381.  
  2382.       ;; Declarations, class types and capitalized variables:
  2383.       ;;
  2384.       ;; Declarations are easy to recognize.  Capitalized words
  2385.       ;; followed by a closing parenthesis are treated as casts if they
  2386.       ;; also are followed by an expression.  Expressions beginning with
  2387.       ;; a unary numerical operator, e.g. +, can't be cast to an object
  2388.       ;; type.
  2389.       ;;
  2390.       ;; The path of a fully qualified type, e.g. java.lang.Foo, is
  2391.       ;; fontified in the reference face.
  2392.       ;;
  2393.       ;; An access to a static field, e.g. System.out.println, is
  2394.       ;; not fontified since it can't be distinguished from the
  2395.       ;; usage of a capitalized variable, e.g. Foo.out.println.
  2396.  
  2397.       (list (concat java-font-lock-class-name-regexp
  2398.             "\\s *\\(\\[\\s *\\]\\s *\\)*"
  2399.             "\\(\\<\\|$\\|)\\s *\\([\(\"]\\|\\<\\)\\)")
  2400.         '(1 (save-match-data
  2401.               (save-excursion
  2402.             (goto-char
  2403.              (match-beginning 3))
  2404.             (if (not (looking-at "\\<instanceof\\>"))
  2405.                 'font-lock-type-face))))
  2406.         (list (concat "\\=" java-font-lock-identifier-regexp "\\.")
  2407.               '(progn
  2408.              (goto-char (match-beginning 0))
  2409.              (while (or (= (preceding-char) ?.)
  2410.                     (= (char-syntax (preceding-char)) ?w))
  2411.                (backward-char)))
  2412.               '(goto-char (match-end 0))
  2413.               '(1 font-lock-reference-face)
  2414.               '(0 nil))        ; Workaround for bug in XEmacs.
  2415.         '(font-lock-match-java-declarations
  2416.           (goto-char (match-end 1))
  2417.           (goto-char (match-end 0))
  2418.           (1 font-lock-variable-name-face))))))
  2419.  
  2420.   ;; Modifier keywords and Java doc tags
  2421.   (setq java-font-lock-keywords-3
  2422.     (append
  2423.  
  2424.      '(
  2425.        ;; Feature scoping:
  2426.        ;; These must come first or the Modifiers from keywords-1 will
  2427.        ;; catch them.  We don't want to use override fontification here
  2428.        ;; because then these terms will be fontified within comments.
  2429.        ("\\<private\\>"   0 font-lock-string-face)
  2430.        ("\\<protected\\>" 0 font-lock-preprocessor-face)
  2431.        ("\\<public\\>"    0 font-lock-reference-face))
  2432.      java-font-lock-keywords-2
  2433.  
  2434.      (list
  2435.  
  2436.       ;; Java doc tags
  2437.       '("@\\(author\\|exception\\|param\\|return\\|see\\|version\\)\\s "
  2438.         0 font-lock-keyword-face t)
  2439.  
  2440.       ;; Doc tag - Parameter identifiers
  2441.       (list (concat "@param\\s +" java-font-lock-identifier-regexp)
  2442.         1 'font-lock-variable-name-face t)
  2443.  
  2444.       ;; Doc tag - Exception types
  2445.       (list (concat "@exception\\ s*"
  2446.             java-font-lock-identifier-regexp)
  2447.         '(1 (if (eq (char-after (match-end 0)) ?.)
  2448.             font-lock-reference-face font-lock-type-face) t)
  2449.         (list (concat "\\=\\." java-font-lock-identifier-regexp)
  2450.               '(goto-char (match-end 0)) nil
  2451.               '(1 (if (eq (char-after (match-end 0)) ?.)
  2452.                   'font-lock-reference-face 'font-lock-type-face) t)))
  2453.  
  2454.       ;; Doc tag - Cross-references, usually to methods 
  2455.       '("@see\\s +\\(\\S *[^][ \t\n\r\f(){},.;:]\\)"
  2456.         1 font-lock-function-name-face t)
  2457.  
  2458.       )))
  2459.   )
  2460.  
  2461. (defvar java-font-lock-keywords java-font-lock-keywords-1
  2462.   "Additional expressions to highlight in Java mode.")
  2463.  
  2464. ;; Match and move over any declaration/definition item after
  2465. ;; point.  Does not match items which look like a type declaration
  2466. ;; (primitive types and class names, i.e. capitalized words.)
  2467. ;; Should the variable name be followed by a comma, we reposition
  2468. ;; the cursor to fontify more identifiers.
  2469. (defun font-lock-match-java-declarations (limit)
  2470.   "Match and skip over variable definitions."
  2471.   (if (looking-at "\\s *\\(\\[\\s *\\]\\s *\\)*")
  2472.       (goto-char (match-end 0)))
  2473.   (and
  2474.    (looking-at java-font-lock-identifier-regexp)
  2475.    (save-match-data
  2476.      (not (string-match java-font-lock-type-regexp
  2477.             (buffer-substring (match-beginning 1)
  2478.                       (match-end 1)))))
  2479.    (save-match-data
  2480.      (save-excursion
  2481.        (goto-char (match-beginning 1))
  2482.        (not (looking-at
  2483.          (concat java-font-lock-class-name-regexp
  2484.              "\\s *\\(\\[\\s *\\]\\s *\\)*\\<")))))
  2485.    (save-match-data
  2486.      (condition-case nil
  2487.      (save-restriction
  2488.        (narrow-to-region (point-min) limit)
  2489.        (goto-char (match-end 0))
  2490.        ;; Note: Both `scan-sexps' and the second goto-char can
  2491.        ;; generate an error which is caught by the
  2492.        ;; `condition-case' expression.
  2493.        (while (not (looking-at "\\s *\\(\\(,\\)\\|;\\|$\\)"))
  2494.          (goto-char (or (scan-sexps (point) 1) (point-max))))
  2495.        (goto-char (match-end 2)))   ; non-nil
  2496.        (error t)))))
  2497.  
  2498.  
  2499. (defvar tex-font-lock-keywords
  2500. ;  ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
  2501. ;  '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
  2502. ;     2 font-lock-function-name-face)
  2503. ;    ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
  2504. ;     2 font-lock-reference-face)
  2505. ;    ;; It seems a bit dubious to use `bold' and `italic' faces since we might
  2506. ;    ;; not be able to display those fonts.
  2507. ;    ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
  2508. ;    ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
  2509. ;    ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
  2510. ;    ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
  2511.   ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
  2512.   '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
  2513.      2 font-lock-function-name-face)
  2514.     ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
  2515.      2 font-lock-reference-face)
  2516.     ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
  2517.     "\\\\\\([a-zA-Z@]+\\|.\\)"
  2518.     ;; It seems a bit dubious to use `bold' and `italic' faces since we might
  2519.     ;; not be able to display those fonts.
  2520.     ;; LaTeX2e: \emph{This is emphasized}.
  2521.     ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
  2522.     ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
  2523.     ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
  2524.      3 (if (match-beginning 2) 'bold 'italic) keep)
  2525.     ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for good tables.
  2526.     ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
  2527.      3 (if (match-beginning 2) 'bold 'italic) keep))
  2528.   "Default expressions to highlight in TeX modes.")
  2529.  
  2530. ;; The previous version, before replacing it with the FSF version.
  2531. ;(defconst tex-font-lock-keywords (purecopy
  2532. ;  (list
  2533. ;   ;; Lionel Mallet: Thu Oct 14 09:41:38 1993
  2534. ;   ;; I've added an exit condition to the regexp below, and the other
  2535. ;   ;; regexps for the second part.
  2536. ;   ;; What would be useful here is something like:
  2537. ;   ;; ("\\(\\\\\\w+\\)\\({\\(\\w+\\)}\\)+" 1 font-lock-keyword-face t 3
  2538. ;   ;;  font-lock-function-name-face t)
  2539. ;   '("\\(\\\\\\w+\\)\\W" 1 font-lock-keyword-face t)
  2540. ;   '("\\(\\\\\\w+\\){\\([^}\n]+\\)}" 2 font-lock-function-name-face t)
  2541. ;   '("\\(\\\\\\w+\\){\\(\\w+\\)}{\\(\\w+\\)}" 3
  2542. ;     font-lock-function-name-face t)
  2543. ;   '("\\(\\\\\\w+\\){\\(\\w+\\)}{\\(\\w+\\)}{\\(\\w+\\)}" 4
  2544. ;     font-lock-function-name-face t)
  2545. ;   '("{\\\\\\(em\\|tt\\)\\([^}]+\\)}" 2 font-lock-comment-face t)
  2546. ;   '("{\\\\bf\\([^}]+\\)}" 1 font-lock-keyword-face t)
  2547. ;   '("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)\\W" 1 font-lock-function-name-face t)
  2548. ;   ;; Lionel Mallet: Thu Oct 14 09:40:10 1993
  2549. ;   ;; the regexp below is useless as it is now covered by the first 2 regexps
  2550. ;   ;;   '("\\\\\\(begin\\|end\\){\\([a-zA-Z0-9\\*]+\\)}"
  2551. ;   ;;     2 font-lock-function-name-face t)
  2552. ;   '("[^\\\\]\\$\\([^$]*\\)\\$" 1 font-lock-string-face t)
  2553. ;;   '("\\$\\([^$]*\\)\\$" 1 font-lock-string-face t)
  2554. ;   ))
  2555. ;  "Additional expressions to highlight in TeX mode.")
  2556.  
  2557. (defconst ksh-font-lock-keywords (purecopy
  2558.   (list
  2559.    '("\\(^\\|[^\$\\\]\\)#.*" . font-lock-comment-face)
  2560.    '("\\<\\(if\\|then\\|else\\|elif\\|fi\\|case\\|esac\\|for\\|do\\|done\\|foreach\\|in\\|end\\|select\\|while\\|repeat\\|time\\|function\\|until\\|exec\\|command\\|coproc\\|noglob\\|nohup\\|nocorrect\\|source\\|autoload\\|alias\\|unalias\\|export\\|set\\|echo\\|eval\\|cd\\|log\\|compctl\\)\\>" . font-lock-keyword-face)
  2561.    '("\\<\\[\\[.*\\]\\]\\>" . font-lock-type-face)
  2562.    '("\$\(.*\)" . font-lock-type-face)
  2563.    ))
  2564.   "Additional expressions to highlight in ksh-mode.")
  2565.  
  2566. (defconst sh-font-lock-keywords (purecopy
  2567.   (list
  2568.    '("\\(^\\|[^\$\\\]\\)#.*" . font-lock-comment-face)
  2569.    '("\\<\\(if\\|then\\|else\\|elif\\|fi\\|case\\|esac\\|for\\|do\\|done\\|in\\|while\\|exec\\|export\\|set\\|echo\\|eval\\|cd\\)\\>" . font-lock-keyword-face)
  2570.    '("\\[.*\\]" . font-lock-type-face)
  2571.    '("`.*`" . font-lock-type-face)
  2572.    ))
  2573.   "Additional expressions to highlight in sh-mode.")
  2574.  
  2575.  
  2576. ;; Install ourselves:
  2577.  
  2578. (add-hook 'find-file-hooks 'font-lock-set-defaults t)
  2579.  
  2580. (make-face 'font-lock-comment-face "Face to use for comments.")
  2581. (make-face 'font-lock-doc-string-face "Face to use for documentation strings.")
  2582. (make-face 'font-lock-string-face "Face to use for strings.")
  2583. (make-face 'font-lock-keyword-face "Face to use for keywords.")
  2584. (make-face 'font-lock-function-name-face "Face to use for function names.")
  2585. (make-face 'font-lock-variable-name-face "Face to use for variable names.")
  2586. (make-face 'font-lock-type-face "Face to use for type names.")
  2587. (make-face 'font-lock-reference-face "Face to use for reference names.")
  2588. (make-face 'font-lock-preprocessor-face
  2589.        "Face to use for preprocessor commands.")
  2590.  
  2591. ;; Backwards compatibility?
  2592.  
  2593. (if (eq t font-lock-use-colors)
  2594.     (setq font-lock-use-colors '(color)))
  2595.  
  2596. (if (eq t font-lock-use-fonts)
  2597.     (setq font-lock-use-fonts '(or (mono) (grayscale))))
  2598.  
  2599. (font-lock-apply-defaults 'font-lock-add-fonts font-lock-use-fonts)
  2600. (font-lock-apply-defaults 'font-lock-add-colors font-lock-use-colors)
  2601.  
  2602. ;;;###autoload
  2603. (add-minor-mode 'font-lock-mode " Font")
  2604.  
  2605. ;; Provide ourselves:
  2606.  
  2607. (provide 'font-lock)
  2608.  
  2609. ;;; font-lock.el ends here
  2610.